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

Posted

技术标签:

【中文标题】我有一个这样的数组对象,我想根据 _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 【发布时间】:2018-02-23 04:40:20 【问题描述】:
[
    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】:

您可以将自定义函数传递给Array#sort 方法。这个函数的返回值决定了元素的顺序。

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】:

定义用于排序数组的比较函数。在比较函数中返回键 '_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】:

如果您需要按多列排序并更改升序和降序,则可以使用此通用实现:

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>

【讨论】:

以上是关于我有一个这样的数组对象,我想根据 _0 属性对该数组进行排序,请在 Javascript 或 Dojo 中提出一些解决方案的主要内容,如果未能解决你的问题,请参考以下文章

根据对象属性删除数组元素

检测二维数组中具有共同属性的连续对象

PHP - 按对象属性对对象数组进行排序[重复]

如何过滤对象数组并根据特定属性变得不同

根据数组中的对象属性生成带有头部的列表

根据字符串属性按字母顺序对对象数组进行排序