admin管理员组文章数量:1290934
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.
I'm making an API call; 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; so I need to pass my own function to sort it; however this function doesn't quite do it.
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.
I'm making an API call; 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; so I need to pass my own function to sort it; however this function doesn't quite do it.
Share Improve this question edited Feb 11, 2018 at 20:49 Will asked Feb 6, 2018 at 3:18 WillWill 1791 gold badge3 silver badges13 bronze badges 5- 2 This isn't quite a nested array, it's an array with a single object in it, that object having properties 0-9 which are arrays. In javascript object keys cannot be ordered. You might want to parse the response from your api into a true array. – IrkenInvader Commented Feb 6, 2018 at 3:22
-
1
The
Array.prototye.sort
callback should return an integer, typically in the range of-1
to1
. You are returning a boolean – Phil Commented Feb 6, 2018 at 3:23 - 1 Please show how the expected output will look like – brk Commented Feb 6, 2018 at 3:26
- Your example code is now no longer valid JavaScript. What do you actually have? – Phil Commented Feb 11, 2018 at 20:44
- Updated again... When I print to console, this is exactly what I see. – Will Commented Feb 11, 2018 at 20:50
1 Answer
Reset to default 10You'll want to access the first array entry (since you only have one) and then Array.prototype.sort()
with a standard numeric parator using the first array item of each entry.
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 parator, it's best to read the documentation...
If pareFunction is supplied, the array elements are sorted according to the return value of the pare function. If
a
andb
are two elements being pared, then:
- If
pareFunction(a, b)
is less than 0, sorta
to an index lower thanb
, i.e.a
es first.- If
pareFunction(a, b)
returns 0, leavea
andb
unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.- If
pareFunction(a, b)
is greater than 0, sortb
to an index lower thana
, i.e.b
es first.pareFunction(a, b)
must always return the same value when given a specific pair of elementsa
andb
as its two arguments. If inconsistent results are returned then the sort order is undefined.
本文标签: javascriptSort an array of arrays by the first elements in the nested arraysStack Overflow
版权声明:本文标题:javascript - Sort an array of arrays by the first elements in the nested arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503624a2382191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论