Sort list by data- attribute


/ Published in: JavaScript
Save to your folder(s)

Sorts a list by its 'data-' attribute. Customization is very easy.


Copy this code and paste it in your HTML
  1. (function () {
  2. var lis_bucket = [],
  3. lis_list = $('li.tab-list-item'),
  4. list_length = lis_list.length,
  5. ul = $('.events-content'),
  6. today = new Date(),
  7. i = 0;
  8.  
  9. for (i = 0; i < list_length; i += 1) {
  10. lis_bucket[i] = [];
  11. lis_bucket[i][0] = new Date($(lis_list[i]).attr('data-date'));
  12. lis_bucket[i][1] = $(lis_list[i]).remove();
  13. }
  14.  
  15. lis_bucket.sort(function (a, b) {
  16. return a[0] - b[0];
  17. });
  18.  
  19. for (i = 0; i < list_length; i += 1) {
  20. if (today < lis_bucket[i][0]){
  21. ul.append(lis_bucket[i][1]);
  22. }
  23. }
  24.  
  25. }());

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.