0

If I have an unordered list:

<ul>
 <li data-sort-name="a">A</li>
 <li data-sort-name="d">D</li>
 <li data-sort-name="b">B</li>
 <li data-sort-name="c">C</li>
</ul>

and an array:

[a,b,c,d]

Is there a way to sort the above list in the same order as the array. (Array isn't always chronological, that's just for explanation purposes).

Thanks.

2 Answers 2

5

One possible jQuery solution:

$.each(['a', 'b', 'c', 'd'], function(i, v) {
    $('li[data-sort-name="' + v + '"]').appendTo('ul');
});

DEMO: http://jsfiddle.net/8FR8V/

Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend a library like underscore for functionality like that. They're small, fast, and very useful!

Underscore sorting functionality: http://underscorejs.org/#sortBy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.