7

I'd like to sort DOM elements that has data attributes defined for rating and date. What is the best way to implement sorting on the front-end with jQuery?

Sample code can be seen on: http://jsfiddle.net/gercheq/zhqXd/

Here is the desired functionality implemented with tables: http://tablesorter.com/docs/

Thanks,

2 Answers 2

8

There is a cool jQuery plugin that sorts DOM elements by attribute. You can find it here: http://tinysort.sjeiti.com/

Example implementation: http://jsfiddle.net/statico/JNFFj/7/

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

1 Comment

I've been looking for something EXACTLY like this. Awesome to see I dont have to start from scratch!
4

Here is the basic idea...

var sortedSet = $('#sort li').toArray().sort(function(a, b) {
   return $(a).data('rating') - $(b).data('rating');
});

You select the elements, convert them to a proper array, and then sort (the comparison function I used is an example, change it to suit your requirements).

jsFiddle with lowest button.

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.