Assume I have an array of options to sort by multiple scores, and those scores are an array of variable length
[
#[Label, scores]
["Label6", [1]
["Label5", [1,5]
["Label2", [0,1,3,5],
["Label1", [0,1,2]
["Label3", [0,1,4],
["Label4", [1,4]
]
I want to sort this list by evaluating array elements one by one. IE. I will first sort (ascending) by the score[0], then score[i+1] if it exists, etc. When score[i] does not exist, it should be considered as an infinite value (appear at the bottom)
This could gives the following output on the previous data :
list = [
#[Label, scores]
["Label1", [0,1,2]
["Label2", [0,1,3,5],
["Label3", [0,1,4],
["Label4", [1,4]
["Label5", [1,5]
["Label6", [1]
]
How can I write this function ? Is there a native/ES6 (or lodash/underscore) sortBy() function like we have in other languages ? (for example in Ruby you would simply list.sort_by(&:last) to get this effect). Or does it need to be implemented manually ?
.sortfunction. All solutions I came across involved something likesort((a,b) => if a[0] - b[0], a[1] - b[1], ...but what happens when an "item of sort criteria" can be of variable length ?0, 1, 2to appear after0, 1, 2, 3?