简体   繁体   English

根据值对对象数组进行排序

[英]sort an array of object accoring to the values

I have this array of objects: 我有这个对象数组:

const array =[ 
  { a: 'good car' },
  { a: 'good car1' },
  { a: 'good car2' },
  { a: 'good car3' },
  { a: 'good car4' },
  { b: 'good car1' } 
];

I need to sort it via values except in the case of ties. 除领带的情况外,我需要通过值对它进行排序。 In the case of ties, the key is used to break the tie. 如果是平局,则使用key打破平局。 I searched A LOT but couldn't use the answers as my keys in objects are not the same and also in case of the tie (values are the same), I cannot handle that. 我搜索了很多但无法使用答案,因为我的对象中的键不相同,并且在领带(值相同)的情况下,我也无法处理。 Any suggestions? 有什么建议么?

You could get the entries, pick the first one and destructure it to key and value and take the same for the other object, then return the chained sorting values. 您可以获取条目,选择第一个条目并将其解构为键和值,然后将其用于另一个对象,然后返回链接的排序值。

 var array = [{ v21: 'sad sdd' }, { aaa: 'sad sdd' }, { v11: 'r err rr' }, { hf32: 'erwwer fgh' }, { z3f2: 'agfrr vbbar' }, { t142: 'gggoog anfa' }, { u23: 'fa handle err' }]; array.sort((a, b) => { var [keyA, valueA] = Object.entries(a)[0], [keyB, valueB] = Object.entries(b)[0]; return valueA.localeCompare(valueB) || keyA.localeCompare(keyB); }); console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

use sort from es6. 使用sort从ES6。

try this code: 试试这个代码:

const array =[ 
  { v21: 'sad sdd' },
  { v11: 'r err rr' },
  { hf32: 'erwwer fgh' },
  { z3f2: 'agfrr vbbar' },
  { t142: 'agfrr vbbar' },
  { u23: 'fa handle err' } 
]

array.sort( (x,y) => { 
  if ( Object.values(x)[0] > Object.values(y)[0] )
    return 1
  else if ( Object.values(x)[0] < Object.values(y)[0] )
    return -1
  else {
    if ( Object.keys(x)[0] > Object.keys(y)[0] )
      return 1
    else if ( Object.keys(x)[0] < Object.keys(y)[0] )
      return -1
  }
})

You can try the following code: 您可以尝试以下代码:

 const array =[ { v21: 'sad sdd' }, { v11: 'r err rr' }, { hf32: 'erwwer fgh' }, { z3f2: 'agfrr vbbar' }, { z3f1: 'agfrr vbbar' }, { t142: 'gggoog anfa' }, { u23: 'fa handle err' } ]; array.sort((a,b) => { if (a[Object.keys(a)[0]] === b[Object.keys(b)[0]]) { return Object.keys(a)[0].localeCompare(Object.keys(b)[0]); } return a[Object.keys(a)[0]].localeCompare(b[Object.keys(b)[0]]); }); console.log(array); 

A variant without using compare function: 不使用比较功能的变体:

const array = [
  { v21: "sad sdd" },
  { v11: "r err rr" },
  { hf32: "erwwer fgh" },
  { z3f2: "sad sdd" },
  { t142: "gggoog  anfa" },
  { u23: "fa handle err" }
];

const result = array
  .map(item => ({
    key: Object.keys(item)[0],
    value: item[Object.keys(item)[0]]
  }))
  .map(item => `${item.value}:${item.key}`)
  .sort()
  .map(item => item.split(":"))
  .map(item => ({ [item[1]]: item[0] }));

console.log(result);

have to use maybe some other join char instead of ':' if it is expected in the value 如果必须在值中使用,则必须使用其他一些连接字符代替“:”

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

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