Mark W
Mark W

Reputation: 5964

jquery sort multiple lists

I would like to sort several ULs with jquery. The criteria is that the LI containing the text "current" must be moved to the top of that list.

example

<ul>
    <li>doc1</li>
    <li>doc5</li>
    <li>doc3</li>
    <li>doc54 (current)</li>
    <li>doc1</li>
</ul>

would become

<ul>
    <li>doc54 (current)</li>
    <li>doc1</li>
    <li>doc5</li>
    <li>doc3</li>
    <li>doc1</li>
</ul>

(but there are many of these lists on the page, all different and not always containing an item which is labeled "current" which all must be sorted)

Help would be appreciated. JSFiddles even more appreciated.

Upvotes: 0

Views: 215

Answers (1)

Yoshi
Yoshi

Reputation: 54649

Try something like:

$('li:contains("current")').each(function () {
  $(this).insertBefore(
    $(this).parent().children().not(this).eq(0)
  );
});

http://jsfiddle.net/D2EkM/4/

Upvotes: 4

Related Questions