Sorting a list by data-attribute

后端 未结 3 1423
借酒劲吻你
借酒劲吻你 2020-12-20 02:16

I have a list of people with job titles sorted by the persons’ first names, like this:

3条回答
  •  轮回少年
    2020-12-20 03:01

    You can pass a comparison function to Array.prototype.sort

    you should be able to do something like

    $items = $('li[data-azsort]');
    var compareElem = function (a, b) {
      if (a.attr('data-azsort') > b.attr('data-azsort') {
        return 1;
      } else {
       return -1
      }
    };
    Array.prototype.sort.apply($items, compareElem);
    

提交回复
热议问题