HTML5 Data Attribute Sort

I would like to sort DOM elements that have data attributes defined for rating and date. What is the best way to implement sorting in an interface using jQuery?

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

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

Thanks,

+7
source share
2 answers

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

Implementation example: http://jsfiddle.net/statico/JNFFj/7/

+8
source

Here is the main idea ...

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

You select the elements, convert them to the correct array, and then sort them (using the comparison function that I used, changing it according to your requirements).

jsFiddle with the lowest button .

+4
source

All Articles