Sorting a list by data-attribute

后端 未结 3 1425
借酒劲吻你
借酒劲吻你 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 02:41

    What about getting all of the list items, push them into array which later will be sorted?

    var allListElements = document.getElementById("staff").getElementsByTagName("li");
    var staff = new Array();
    for (i = 0; i < allListElements.length; i++) {
      staff.push(allListElements[i].getAttribute('data-azsort'));
    }
    
    staff.sort(function(a, b) {
      if (a < b) return -1;
      if (a > b) return 1;
      return 0;
    });
    
    //Print
    
    document.write('

    Sorted

    '); for (i = 0; i < staff.length; i++) { document.write(staff[i] + "
    "); }

    Input

    Additionally you can save the index of

  • and reorder the
      .

提交回复
热议问题