Javascript 如何根据对象的属性对对象数组进行排序

Javascript 如何根据对象的属性对对象数组进行排序,javascript,node.js,Javascript,Node.js,我有一个对象数组。数组中的每个对象都有一个名为“priorityTag”的属性 我想根据“priorityTag”对数组进行升序排序。 所以,我写了下面发布的代码,但它不起作用 请告诉我如何根据“priorityTag”对数组进行排序 注意:priortyTag是数组中包含的对象的属性。换句话说,数组中的每个元素都是一个对象,每个对象都有很多属性,但其中一个属性是“PriorityTag” 我想根据“priorityTag”对数组进行排序 代码: sortFeaturesPerPriority2

我有一个对象数组。数组中的每个对象都有一个名为“priorityTag”的属性

我想根据“priorityTag”对数组进行升序排序。 所以,我写了下面发布的代码,但它不起作用

请告诉我如何根据“priorityTag”对数组进行排序

注意:priortyTag是数组中包含的对象的属性。换句话说,数组中的每个元素都是一个对象,每个对象都有很多属性,但其中一个属性是“PriorityTag”

我想根据“priorityTag”对数组进行排序

代码

sortFeaturesPerPriority2(arr) {
logger.debug('[sortFeaturesPerPriority2]: arr:', arr);

 arr.sort((a, b) => a.priorityTag - b.priorityTag);
 }

要做到这一点,你可以使用我做的原型

/**
 * ARRAY PROTOTYPE SORT BY
 * Used to sort an array by object field
 * @param {String} field
 */
Array.prototype.sortBy = function (field = '') {
    this.sort(function (a, b) {
        return a[field] == b[field] ? 0 : +( a[field] > b[field]) || -1;
    });
};
您可以这样使用:

let array = [{name: 'Jonh', age: 71}, {name: 'Tracey', age: 58}];
array.sortBy('age');
console.log(array); // Display [ { name: 'Tracey', age: 58 }, { name: 'Jonh', age: 71 } ]

希望有帮助。

假设您在
priorityTag
s中有
整数,比如
变量,您需要在排序之前解析它们

sortFeaturesPerPriority2(arr) {
    logger.debug('[sortFeaturesPerPriority2]: arr:', arr);

     arr.sort(({priorityTag: p1}, {priorityTag: p2}) => parseInt(p1) - parseInt(p2));
 }
如果无法将priorityTag解析为数值(假设您有
priorityTag=='a'
priorityTag=='b'
),您可以查看@sander在注释中提供的

编辑 不是真的回答OP的问题,但这里有一种通过属性定义各种排序的简洁方法:

const arr = [
  {priorityTag: 'a'}, 
  {priorityTag: 'e'},
  {priorityTag: 'd'},
  {priorityTag: 'b'},
  {priorityTag: 'c'}, 
];

const numericSort = prop => ({[prop]: a}, {[prop]: b}) => a - b;
const alphaSort = prop => ({[prop]: a}, {[prop]: b}) => a < b ? -1 : 1;
const weirdCustomSort = prop => ({[prop]: a}, {[prop]: b}) => {
  const elements = ['a','e','b','d','c'];
  const aIndex = ~elements.indexOf(a) ? elements.indexOf(a) : elements.length;
  const bIndex = ~elements.indexOf(b) ? elements.indexOf(b) : elements.length;
  return aIndex - bIndex;
};

const compareBy = prop => sortFunc => sortFunc(prop)

console.log([...arr].sort(compareBy('priorityTag')(alphaSort)))
console.log([...arr].sort(compareBy('priorityTag')(numericSort)))
console.log([...arr].sort(compareBy('priorityTag')(weirdCustomSort)))
const arr=[
{priorityTag:'a'},
{priorityTag:'e'},
{priorityTag:'d'},
{priorityTag:'b'},
{priorityTag:'c'},
];
常量numericSort=prop=>({[prop]:a},{[prop]:b})=>a-b;
常量alphaSort=prop=>({[prop]:a},{[prop]:b})=>a({[prop]:a},{[prop]:b})=>{
常量元素=['a'、'e'、'b'、'd'、'c'];
const aIndex=~elements.indexOf(a)?elements.indexOf(a):elements.length;
const bIndex=~elements.indexOf(b)?elements.indexOf(b):elements.length;
返回aIndex-bIndex;
};
const compareBy=prop=>sortFunc=>sortFunc(prop)
console.log([…arr].sort(compareBy('priorityTag')(alphaSort)))
console.log([…arr].sort(compareBy('priorityTag')(numericSort)))
console.log([…arr].sort(compareBy('priorityTag')(weirdCustomSort)))

假设优先级标记是一个数字,那么这看起来是正确的。我试过了那些代码,它命令了它们

const arr = [
  {priorityTag: 1},
  {priorityTag: 5},
  {priorityTag: 0},
  {priorityTag: 2},
  {priorityTag: 6},
]
arr.sort( (a,b) => a.priorityTag - b.priorityTag ); // 0,1,2,5,6
如果priorityTag是一个字符串,那么我将只使用以下内容。这实际上适用于字符串和数字,前提是您确定“大于”在默认情况下如何处理字符串

arr.sort( (a,b) => a.priorityTag > b.priorityTag );

priorityTag中有什么我猜这就是你在找的for@Logar我编辑了我的问题,如果优先级标记是一个数字,那么你的问题应该有效。是的,它应该仍然有效,数组是通过引用传递的。为了确保,您能在
arr.sort之后立即记录数组吗(..
在调用
sortFeaturesPerPriority2
之后,我相信比较函数必须返回一个整数,不确定布尔值的行为是什么,但我记得我在使用布尔值返回时遇到了问题type@logar嗯,说得好。实际上我没有意识到。我猜布尔值只是被转换成了in至少对于这个简单的例子来说,teger隐式地?似乎是有效的,但我会在将来密切关注。