How to sort an array in a unique order

后端 未结 4 1965
暗喜
暗喜 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:49

    You could use filter to get the special values, sort the rest, and concatenate the parts:

    var myList = [ 'Normal', 'Urgent', 'Alert', 'Casual', 'Follow up' ];
    
    myList = myList.filter( v => v === 'Urgent' ).concat(
                myList.filter( v => v === 'Alert' ),
                myList.filter( v => !['Alert','Urgent'].includes(v) ).sort());
    
    console.log(myList);

提交回复
热议问题