简体   繁体   English

特殊排序对象数组

[英]Special sorting of an array of objects

So theres tons of posts on sorting something like this: 因此,有大量关于此类排序的帖子:

var arr = [{a: 1}, {b: 2}] alphabetically by key, but what if you have something like var arr = [{a: 100}, {a: 50}] , what I want is to then say "oh you're the same? lets sort you then by value (which will always be a number). var arr = [{a: 1}, {b: 2}]按字母顺序排列,但是如果您有类似var arr = [{a: 100}, {a: 50}]东西,该怎么办呢?说“哦,你是同一个人?让我们按值对您进行排序(该值始终是数字)。

I am unsure how to do either in lodash or any other similar javascript way. 我不确定如何用lodash或任何其他类似的javascript方式进行操作。

The end result should be either: 最终结果应为:

[{b: 2}, {a: 1}] // Keys are different (alphabetical)
// or:
[{a: 50}, {a: 100}] // Keys are the same (lowest to highest)

Everything I have seen on stack overflow becomes a quick mess (code wise), and I was thinking, there are sort methods on lodash, but how exactly do I achieve what I want given the circumstances ?? 我在堆栈溢出上看到的所有内容都变得一团糟(代码明智),我当时在想,lodash上有排序方法,但是在这种情况下,我如何准确地实现所需的目标?

Any ideas? 有任何想法吗?

Some one asked a good question, are there more keys? 有人问了一个好问题,还有更多的钥匙吗? Are they only a and b ? 他们只是ab吗?

There would only be two objects in this array at any given time and yes the keys would only ever be strings, in this case the strings could be anything, cat, dog, mouse, apple, banana ... What ever. 在任何给定时间,此数组中将只有两个对象,是的,键只能是字符串,在这种情况下,字符串可以是猫,狗,鼠标,苹果,香蕉等任何东西。

The values will only ever be numbers. 这些值将永远只能是数字。

Clearing the air 清除空气

If the keys match, only sort the array by value, if the keys do not match, only sort the array by key. 如果键匹配,则仅按值对数组排序;如果键不匹配,则仅按键对数组排序。 There will only ever be two objects in this array. 该数组中将永远只有两个对象。 Apologies for the misunderstanding. 对于误解表示歉意。

In case you always have one property in your objects you can first sort by key using localeCompare and then by value of that property. 如果对象中始终有一个属性,则可以首先使用localeCompare key排序,然后按该属性的值排序。

 var arr = [{b: 2}, {b: 10}, {a: 1}, {c: 1}, {a: 20}] arr.sort(function(a, b) { var kA = Object.keys(a)[0] var kB = Object.keys(b)[0] return kA.localeCompare(kB) || a[kA] - b[kB] }) console.log(arr) 

Before sorting you can create array of unique keys that you can use to check if all object have the same key by checking if length is > 1 and use that in sort function. 在排序之前,您可以创建唯一键数组,可用于通过检查length是否大于1来检查所有对象是否具有相同的键,并在sort函数中使用该键。

 var arr = [{b: 10}, {b: 2}, {a: 1}, {c: 1}, {a: 20}, {b: 22}] var arr2 = [{a: 10}, {a: 2}, {a: 1}, {a: 22}] function customSort(data) { var keys = [...new Set([].concat(...data.map(e => Object.keys(e))))] data.sort(function(a, b) { var kA = Object.keys(a)[0] var kB = Object.keys(b)[0] return keys.length > 1 ? kA.localeCompare(kB) : a[kA] - b[kB] }) return data; } console.log(customSort(arr)) console.log(customSort(arr2)) 

You can use only one function to perform the two types of sorting (works for your case, in which you have only an array with two items, but it is completely generic regarding the array length): 您只能使用一个函数来执行两种类型的排序(适合您的情况,其中只有一个包含两个项目的数组,但是对于数组长度而言,这是完全通用的):

 var arr1 = [{a: 30}, {a: 2}]; var arr2 = [{b: 30}, {a: 2}]; function sortArr(arr) { return arr.sort((a, b) => { var aKey = Object.keys(a)[0]; var bKey = Object.keys(b)[0]; return (aKey !== bKey) ? aKey.localeCompare(bKey) : a[aKey] - b[bKey]; }); } var sortedArr1 = sortArr(arr1); var sortedArr2 = sortArr(arr2); console.log(sortedArr1); console.log(sortedArr2); 

 var arr = [{b: 2}, {a: 1}, {b: 1}, {a: 100}, {a: 20}]; arr.sort(function(a, b) { var aKey = a.hasOwnProperty("a")? "a": "b", // get the first object key (if it has the property "a" then its key is "a", otherwise it's "b") bKey = b.hasOwnProperty("a")? "a": "b"; // same for the second object return aKey === bKey? // if the keys are equal a[aKey] - b[aKey]: // then sort the two objects by the value of that key (stored in either aKey or bKey) aKey.localeCompare(bKey); // otherwise sort by the strings aKey and bKey (the keys of the two objects) }); console.log(arr); 

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

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