How to sort an array of objects by multiple fields?

后端 未结 30 2319
北恋
北恋 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:17

    homes.sort(function(a,b) { return a.city - b.city } );
    homes.sort(function(a,b){
        if (a.city==b.city){
            return parseFloat(b.price) - parseFloat(a.price);
        } else {
            return 0;
        }
    });
    

提交回复
热议问题