Sort array by firstname (alphabetically) in Javascript

前端 未结 23 3011
天命终不由人
天命终不由人 2020-11-22 11:46

I got an array (see below for one object in the array) that I need to sort by firstname using JavaScript. How can I do it?

var user = {
   bio: null,
   emai         


        
23条回答
  •  半阙折子戏
    2020-11-22 12:13

    underscorejs offers the very nice _.sortBy function:

    _.sortBy([{a:1},{a:3},{a:2}], "a")
    

    or you can use a custom sort function:

    _.sortBy([{a:"b"},{a:"c"},{a:"a"}], function(i) {return i.a.toLowerCase()})
    

提交回复
热议问题