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 to 1. 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
Add a ment  | 

1 Answer 1

Reset to default 10

You'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 and b are two elements being pared, then:

  • If pareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a es first.
  • If pareFunction(a, b) returns 0, leave a and b 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, sort b to an index lower than a, i.e. b es first.
  • pareFunction(a, b) must always return the same value when given a specific pair of elements a and b 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