How to sort an array of objects by multiple fields?

后端 未结 30 2362
北恋
北恋 2020-11-21 11:34

From this original question, how would I apply a sort on multiple fields?

Using this slightly adapted structure, how would I sort city (ascending) & then price (

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 12:26

    Here 'AffiliateDueDate' and 'Title' are columns, both are sorted in ascending order.

    array.sort(function(a, b) {
    
                   if (a.AffiliateDueDate > b.AffiliateDueDate ) return 1;
                   else if (a.AffiliateDueDate < b.AffiliateDueDate ) return -1;
                   else if (a.Title > b.Title ) return 1;
                   else if (a.Title < b.Title ) return -1;
                   else return 0;
                 })
    

提交回复
热议问题