数组排序

it2022-05-05  135

1、普通数组排序:

  ①Array.sort();   //升序

    [5, 4, 3, 2, 1].sort(); ==> [1,2,3,4,5]

  ②Array.sort(function(a,b){return a-b;}) //升序:a-b;降序:b-a; 

    [5, 4, 3, 2, 1].sort(function(a,b){return a-b;})  ==>[1,2,3,4,5]

    [5, 4, 3, 2, 1].sort(function(a,b){return b-a;})  ==>[5, 4, 3, 2, 1]

3、对象数组排序:

  Array.sort(function(a,b){

    return a.Key>b.Key?1:-1;

  })

转载于:https://www.cnblogs.com/ducky-L/p/8067136.html


最新回复(0)