i have a array object like this, i want to sort th

2019-06-14 23:57发布

问题:

[
    {Devices:"All Devices",Groups:"Location/All Locations", id:"table-default00",_0:0},
    {Devices:"All sourecs",Groups:"Location/All Locations", id:"table-default01",_0:1}
]

回答1:

You can pass a custom function to the Array#sort method. The return value of this function decides the order of the elements.

const data = [{
  Devices: "All Devices",
  Groups: "Location/All Locations",
  id: "table-default00",
  _0: 0
}, {
  Devices: "All sourecs",
  Groups: "Location/All Locations",
  id: "table-default01",
  _0: 1
}];

const result = data.sort((a, b) => a['_0'] - b['_0']);
console.log(result);



回答2:

Define compare function for sorting the array. In Compare function return difference of the values of key '_0'.

function sortMyList(a,b){
  return a['_0']-b['_0'];
}

var list = [{Devices:"All sourecs",Groups:"Location/All Locations", id:"table-default01",_0:3},
            {Devices:"All sourecs",Groups:"Location/All Locations", id:"table-default01",_0:2},
            {Devices:"All sourecs",Groups:"Location/All Locations", id:"table-default01",_0:4},
            {Devices:"All Devices",Groups:"Location/All Locations", id:"table-default00",_0:0}, 
            {Devices:"All sourecs",Groups:"Location/All Locations", id:"table-default01",_0:1}];
console.log(list);
list.sort(sortMyList);
console.log(list);


回答3:

You can use this general implementation if you need to sort by multiple columns and change ascending and descending order:

const data = [
    {Devices:"A",Groups:"A", id:"B",_0:3},
    {Devices:"A",Groups:"B", id:"A",_0:2},
    {Devices:"B",Groups:"B", id:"C",_0:1},
    {Devices:"B",Groups:"C", id:"C",_0:0},
];
const DESCENDING = -1;
const ASCENDING = 1;
const sorter = getter => comparer => (a,b) => 
  comparer(getter(a),getter(b));

const compareNumbers = (a,b)=>a-b;

const compareStrings = (a,b)=>a>b?1:(a<b)?-1:0;

const sortDevices = sorter(x=>x.Devices)(compareStrings);
const sortGroups = sorter(x=>x.Groups)(compareStrings);
const sortId = sorter(x=>x.id)(compareStrings);
const sort_0 = sorter(x=>x._0)(compareNumbers);
const sorters = [
  [sort_0,ASCENDING],
  [sortDevices,ASCENDING],
  [sortGroups,ASCENDING],
  [sortId,ASCENDING],
];
var sortCols = [0,1,2,3];
const sort = (sorters) => (a,b) => {
  const recur = (index) =>{
    if(index===sorters.length){
      return 0;
    }
    const [sorter,direction] = sorters[index];
    const result = sorter(a,b);
    if(result!==0){
      return result*direction;
    }
    return recur(index+1);
  }
  return recur(0);
};

const changeSort = index => {
  if(sortCols[0]===index){
    (sorters[sortCols[0]][1]===ASCENDING)
      ?  sorters[sortCols[0]][1]=DESCENDING
      :  sorters[sortCols[0]][1]=ASCENDING
  }else{
    sortCols = [index].concat(sortCols.filter(val=>val!==index));
  }
  showShorted(sortCols.map(i=>sorters[i]));
};

const showShorted = sorters => {
  document.querySelector("#output").innerHTML =
    JSON.stringify(
      data
      .map(x=>x)//copy so we won't change original
      .sort(
        sort(sorters)
      ),
      undefined,
      2
    );
}

document.querySelector("#content").addEventListener(
  "click",
  e=>{
    const index = e.target.getAttribute("x-index");
    if(index){
      changeSort(parseInt(index,10));
    }
  }
);
<div id="content">
  <input type="button" value="_0" x-index="0">
  <input type="button" value="Devices" x-index="1">
  <input type="button" value="Groups" x-index="2">
  <input type="button" value="id" x-index="3">
  <pre id="output">
  </pre>
</div>