简体   繁体   English

我有一个这样的数组对象,我想根据 _0 属性对这个数组进行排序,请在 Javascript 或 Dojo 中提出一些解决方案

[英]i have a array object like this, i want to sort this array based on _0 attribute, please suggest some solution in Javascript or Dojo

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

You can pass a custom function to the Array#sort method.您可以将自定义函数传递给Array#sort方法。 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);

Define compare function for sorting the array.定义用于对数组进行排序的比较函数。 In Compare function return difference of the values of key '_0'.在比较函数中返回键“_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);

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)=>ab; 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM