最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alphabetically Order HTML List with Headers - Stack Overflow

programmeradmin2浏览0评论

I am looking to Alphabetically order an HTML list, but after each letter, there would be a <hr /> tag and a header, indicating the new letter list.

To revise if I wasn't clear enough, I have my list...

<ul>
  <li><a href="#/"> john-smith/</a></li>
  <li><a href="#/"> joe-smith/</a></li>
  <li><a href="#/"> glen-smith/</a></li>
  <li><a href="#/"> daniel-smith/</a></li>
  <li><a href="#/"> johnny-smith/</a></li>
</ul>

I am looking to Alphabetically order an HTML list, but after each letter, there would be a <hr /> tag and a header, indicating the new letter list.

To revise if I wasn't clear enough, I have my list...

<ul>
  <li><a href="#/"> john-smith/</a></li>
  <li><a href="#/"> joe-smith/</a></li>
  <li><a href="#/"> glen-smith/</a></li>
  <li><a href="#/"> daniel-smith/</a></li>
  <li><a href="#/"> johnny-smith/</a></li>
</ul>

And now, I wanted to have some JS code that would organise this list alphabetically, as well as give a header & line for each new letter; thus it would give an oute somewhat similar to:

<ul>
  <hr />
  <h3>D</h3>
  <li><a href="#/"> daniel-smith/</a></li>
  <hr />
  <h3>G</h3>
  <li><a href="#/"> glen-smith/</a></li>
  <hr />
  <h3>J</h3>
  <li><a href="#/"> joe-smith/</a></li>
  <li><a href="#/"> johnny-smith/</a></li>
  <li><a href="#/"> joe-smith/</a></li>
</ul>

I did try to do this myself, but I simply wasn't able to, I'm relatively new to JavaScript! Thanks.

Share Improve this question edited Aug 7, 2016 at 8:14 Mosh Feu 29.4k18 gold badges93 silver badges141 bronze badges asked Aug 7, 2016 at 7:49 DanDan 3541 gold badge3 silver badges14 bronze badges 3
  • First try on your own and e here if you cannot get the approach right. Please do not ask people to just write code for you. – Siddharth Venu Commented Aug 7, 2016 at 7:53
  • @SiddharthVenu I did indeed try to do this, and I wasn't able to. I couldn't find any online resources to help me, nor was I able to do it on my own. – Dan Commented Aug 7, 2016 at 7:54
  • That html structure is invalid – charlietfl Commented Aug 7, 2016 at 10:35
Add a ment  | 

2 Answers 2

Reset to default 3

Since putting h3 and hr inside a ul tag is not valid, I created this style with css. Just added a li node with splitter class.

The solution has 2 steps:

  1. Sort the list (using .sort() method)
  2. Create the titles.

Read the code and let me know if something not clear.

var list = $('ul'),
    items = $('li', list);

// sort the list
var sortedItems = items.get().sort(function(a, b) {
  var aText = $.trim($(a).text().toUpperCase()),
      bText = $.trim($(b).text().toUpperCase());
  
   return aText.localeCompare(bText);
});

list.append(sortedItems); 

// create the titles
var lastLetter = '';
list.find('li').each(function() {
  var $this = $(this),
      text = $.trim($this.text()),
      firstLetter = text[0];

  if (firstLetter != lastLetter) {
    $this.before('<li class="splitter">' + firstLetter);
    lastLetter = firstLetter;
  }
});
.splitter {
  border-top: 1px solid;
  font-size: 1.25em;
  list-style: none;
  padding-top: 10px;
  text-transform: uppercase;
  padding-bottom: 10px;
  margin-top: 10px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><a href="#/"> john-smith/</a></li>
  <li><a href="#/"> joe-smith/</a></li>
  <li><a href="#/"> glen-smith/</a></li>
  <li><a href="#/"> daniel-smith/</a></li>
  <li><a href="#/"> johnny-smith/</a></li>
</ul>

You need jquery and the following code, but please be sure to remove the space from each row of the above list (before the names) :

var a = [];
//Loop through each row
$( "li" ).each(function( index ) {
  //populate the array with their names
  a.push($( this ).text());
});
//Alphabetically sort the array
a.sort();
//Clear the existing list
$("ul").html("");
//Start with a random character
var current_letter='1';
//loop through each row of the sorted array
for (var i = 0; i < a.length; i++) {
  //if a new first character is detected, add its corresponding html
   if(a[i].charAt(0)!=current_letter){
       $("ul").append("<hr /><h3>"+a[i].charAt(0)+"</h3>");
       current_letter=a[i].charAt(0);
   }
    //add the list item to the list
    $("ul").append("<li><a href='#/'>"+a[i]+"</a></li>");
}
发布评论

评论列表(0)

  1. 暂无评论