Sort array by firstname (alphabetically) in Javascript

前端 未结 23 2882
天命终不由人
天命终不由人 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:16

    also for both asec and desc sort, u can use this : suppose we have a variable SortType that specify ascending sort or descending sort you want:

     users.sort(function(a,b){
                return   sortType==="asc"? a.firstName.localeCompare( b.firstName): -( a.firstName.localeCompare(  b.firstName));
            })
    

提交回复
热议问题