简体   繁体   English

按两个不同的值对JavaScript对象数组进行排序

[英]Sorting JavaScript Array Of Objects by two different values

I have an object like this in JavaScript: 我在JavaScript中有这样的对象:

myArray[0] -> 0:"62", 1:8, 2:0, 3:"11"
myArray[1] -> 0:"62", 1:8, 2:0, 3:"15"
myArray[2] -> 0:"48", 1:8, 2:0, 3:"04"
myArray[3] -> 0:"48", 1:8, 2:0, 3:"01"
myArray[4] -> 0:"62", 1:8, 2:0, 3:"12"
myArray[5] -> 0:"48", 1:8, 2:0, 3:"02"
myArray[6] -> 0:"62", 1:8, 2:0, 3:"14"
myArray[7] -> 0:"48", 1:8, 2:0, 3:"03"

And I'm trying to make it to be like this: 而我正试图让它像这样:

myArray[0] -> 0:"48", 1:8, 2:0, 3:"01"
myArray[1] -> 0:"48", 1:8, 2:0, 3:"02"
myArray[2] -> 0:"48", 1:8, 2:0, 3:"03"
myArray[3] -> 0:"48", 1:8, 2:0, 3:"04"
myArray[4] -> 0:"62", 1:8, 2:0, 3:"11"
myArray[5] -> 0:"62", 1:8, 2:0, 3:"12"
myArray[6] -> 0:"62", 1:8, 2:0, 3:"14"
myArray[7] -> 0:"62", 1:8, 2:0, 3:"15"

As you see, I'm trying to sort it by myArray[i][0] at first, and then sort by myArray[i][3] based on the myArray[i][0] as index. 如你所见,我首先尝试通过myArray[i][0]进行排序,然后根据myArray[i][0]作为索引对myArray[i][3]进行排序。 I managed to sort by myArray[i][0] using 我设法通过myArray[i][0]使用排序

myObject.sort(function(a, b){ 
    return parseInt(a) - parseInt(b); 
});

How do I accomplish this with no libraries? 如何在没有库的情况下实现这一目标?

I suggest to sort in one run with chained comparison functions. 我建议使用链式比较函数进行一次运行排序。

How Array#sort() works: Array#sort()的工作原理:

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. 如果提供compareFunction ,则根据compare函数的返回值对数组元素进行排序。 If a and b are two elements being compared, then: 如果ab是被比较的两个元素,那么:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, ie a comes first. 如果compareFunction(a, b)小于0,则将compareFunction(a, b)排序为低于b的索引,即a先来。

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. 如果compareFunction(a, b)返回0,则保持ab相对于彼此保持不变,但是对于所有不同的元素进行排序。 Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (eg Mozilla versions dating back to at least 2003) respect this. 注意:ECMAscript标准不保证这种行为,因此并非所有浏览器(例如可追溯到至少2003年的Mozilla版本)都尊重这一点。

  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a . 如果compareFunction(a, b)是大于0,排序b比较低折射率a
  • compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. 当给定一对特定元素a和b作为其两个参数时compareFunction(a, b)必须始终返回相同的值。 If inconsistent results are returned then the sort order is undefined. 如果返回不一致的结果,则排序顺序未定义。

In this case the compare function has two groups, one for sorting index [0] and one sorting index [3] . 在这种情况下,比较函数有两组,一组用于排序索引[0]和一个排序索引[3] If the value at index [0] is equal, the the sorting for index [3] is taken. 如果index [0]值相等,则采用索引[3]的排序。 Both groups are chained with a logical or || 两个组都使用逻辑或||链接 .

 var array = [["62", 8, 0, "11"], ["62", 8, 0, "15"], ["48", 8, 0, "04"], ["48", 8, 0, "01"], ["62", 8, 0, "12"], ["48", 8, 0, "02"], ["62", 8, 0, "14"], ["48", 8, 0, "03"]]; array.sort(function (a, b) { return a[0].localeCompare(b[0]) || a[3].localeCompare(b[3]); }); document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>'); 

To me your variable looks like an Array (object indexed with integer keys) so, if it's the case, what about: 对我来说,你的变量看起来像一个Array (用整数键索引的对象),所以,如果是这样,那么:

var ar = [
  ["62", 8, 0, "11"],
  ["62", 8, 0, "15"],
  ["48", 8, 0, "04"],
  ["48", 8, 0, "01"],
  ["62", 8, 0, "12"],
  ["48", 8, 0, "02"],
  ["62", 8, 0, "14"],
  ["48", 8, 0, "03"]
]

var result =  ar.map(function(a) {
  return {key: a.join(''), val: a}
}).sort(function(a, b){ 
  return parseInt(a.key, 10) - parseInt(b.key, 10);
}).map(function(a) {
  return a.val
})
console.log(result)

See this fiddle 看到这个小提琴

Edit 编辑

The Object version: Object版本:

var data = [{ 0:"62", 1:8, 2:0, 3:"11"},{ 0:"62", 1:8, 2:0, 3:"15"},
            { 0:"48", 1:8, 2:0, 3:"04"},{ 0:"48", 1:8, 2:0, 3:"01"},
            { 0:"62", 1:8, 2:0, 3:"12"},{ 0:"48", 1:8, 2:0, 3:"02"},
            { 0:"62", 1:8, 2:0, 3:"14"},{ 0:"48", 1:8, 2:0, 3:"03"}]

var result =  data.map(function(a) {
  return {key: [0,1,2,3].map(function(k) {return a[k]}).join(''), val: a}
}).sort(function(a, b){ 
  return parseInt(a.key, 10) - parseInt(b.key, 10);
}).map(function(a) {
  return a.val
})
console.log(result)

Updated fiddle 更新了小提琴

Edit 2 编辑2

After @malixsys's comment, here's a more efficient way: 在@ malixsys的评论之后,这是一种更有效的方式:

var result =  data.sort(function(a, b){ 
  return parseInt(a[0]+ a[1] + a[2] + a[3], 10) - parseInt(b[0]+ b[1] + b[2] + b[3], 10);
})

And corresponding updated fiddle 相应更新小提琴

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

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