How to sort an array in a unique order

后端 未结 4 2011
暗喜
暗喜 2021-01-28 10:03

Given an array:

var myList = [ \'Normal\', \'Urgent\', \'Alert\', \'Casual\', \'Follow up\' ];

I want to output this list in say, a dropdown. I

4条回答
  •  难免孤独
    2021-01-28 10:57

    It's best if you keep your prioritized elements separate from the main list, if you can't guarantee they will be present. I would filter those elements out, sort the rest, and concatenate the results with your special list.

    var special = ["Urgent","Alert"];
    var myList = [ 'Normal', 'Urgent', 'Alert', 'Casual', 'Follow up' ];
    
    myList = special.concat(myList.filter(function(el){ return special.indexOf(el) == -1; }).sort());
     
    alert(myList);

提交回复
热议问题