[英]Sort an array of arrays by the first elements in the nested arrays
This is an incredibly basic question that I could do in Python in a matter of seconds- but I'm new to Javascript and maybe I just don't know the nomenclature for the language, but my research hasn't quite answered it. 这是一个非常难以置信的基本问题,我可以在几秒钟内用Python完成-但是我是Java语言的新手,也许我只是不知道该语言的名称,但是我的研究尚未完全回答。
I'm making an API call; 我正在进行API调用; and in response I've got:
作为回应,我得到了:
let unordered_ranges = [[
[1461913200000, 57, 69],
[1380006000000, 75, 79],
[1321344000000, 78, 79],
[1276585200000, 69, 75],
[1252998000000, 68, 76],
[1234512000000, 79, 81],
[1423814400000, 77, 78],
[1489820400000, 69, 79]
]];
The first element in the nested arrays are timestamps in milliseconds. 嵌套数组中的第一个元素是以毫秒为单位的时间戳。 How do I sort the parent array chronologically using the nested timestamps?
如何使用嵌套时间戳按时间顺序对父数组排序?
So far I've got: 到目前为止,我已经:
let ranges= unordered_ranges.sort(function (a, b) {
return a > b
});
I understand .sort() is lexicographic; 我知道.sort()是字典式的; so I need to pass my own function to sort it;
所以我需要传递自己的函数来对其进行排序; however this function doesn't quite do it.
但是此功能不能完全做到这一点。
You'll want to access the first array entry (since you only have one) and then Array.prototype.sort()
with a standard numeric comparator using the first array item of each entry. 您将要访问第一个数组条目(因为只有一个),然后使用每个条目的第一个数组项使用标准数字比较器访问
Array.prototype.sort()
。
let unordered_ranges = [[ [1461913200000, 57, 69], [1380006000000, 75, 79], [1321344000000, 78, 79], [1276585200000, 69, 75], [1252998000000, 68, 76], [1234512000000, 79, 81], [1423814400000, 77, 78], [1489820400000, 69, 79] ]]; let ranges = unordered_ranges[0].sort((a, b) => a[0] - b[0]) console.info(ranges)
To explain the comparator, it's best to read the documentation... 为了解释比较器,最好阅读文档...
If compareFunction is supplied, the array elements are sorted according to the return value of the compare function.
如果提供compareFunction,则根据比较函数的返回值对数组元素进行排序。 If
a
andb
are two elements being compared, then:如果
a
和b
是要比较的两个元素,则:
- If
compareFunction(a, b)
is less than 0, sorta
to an index lower thanb
, iea
comes first.如果
compareFunction(a, b)
小于0,则将compareFunction(a, b)
排序a
小于b
的索引,即a
在前。- If
compareFunction(a, b)
returns 0, leavea
andb
unchanged with respect to each other, but sorted with respect to all different elements.如果
compareFunction(a, b)
返回0,则a
和b
彼此相对不变,但对所有不同元素进行排序。 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, sortb
to an index lower thana
, ieb
comes first.如果
compareFunction(a, b)
是大于0,排序b
到低于一个索引a
,即,b
至上。compareFunction(a, b)
must always return the same value when given a specific pair of elementsa
andb
as its two arguments.当给定一对特定的元素
a
和b
作为其两个参数时compareFunction(a, b)
必须始终返回相同的值。 If inconsistent results are returned then the sort order is undefined.如果返回不一致的结果,则排序顺序不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.