Using Javascript with Underscore.js to Sort the other way

前端 未结 4 839
长情又很酷
长情又很酷 2021-02-01 14:01

I\'m using Javascript sort (with Underscore.js):

_.sortBy([\"Bob\", \"Mary\", \"Alice\"], function (name) {return name})
> [\"Alice\", \"Bob\", \         


        
4条回答
  •  礼貌的吻别
    2021-02-01 14:29

    You can do this with a 1 liner in ES6, just change the > depending on the direction you want.

    .sort() is supported since IE6 and you just pass a function which returns 1 or -1;

    ["Bob", "Mary", "Alice].sort((a, b) => a > b ? 1 : -1);
    

提交回复
热议问题