Monday, 6 June 2016

To sort item alphabetically

To sort item alphabetically using jquery





Html:



        <ul id="list">
            <li id="34">Peter</li>
            <li id="92">Mary</li>
            <li id="49">Paul</li>
            <li id="12">Allen</li>
            <li id="24">James</li>
            <li id="83">Vicki</li>
            <li id="68">Brock</li>
            <li id="1200">Dana</li>
            <li id="56">Frank</li>
            <li id="128">Gil</li>
            <li id="146">Helen</li>
        </ul>
      
jQuery:


var mylist = jQuery('list');
var listitems = mylist.children('li').get();

listitems.sort(function(a, b)
{
   return jQuery(a).text().toUpperCase().localeCompare(jQuery(b).text().toUpperCase());
})

mylist.empty().append(listitems);

//$.each(listitems, function(idx, itm) { mylist.append(itm); });


banner
Previous Post
Next Post

0 comments: