I want to sort a list filled with lists by their 0th index.
In Python, this would look like
l = [[1, 0], [5, 1], [6, 2], [3, 2]
l.sort(key=lambda x: x[0])
#l is now [[1, 0], [3, 2], [5, 1], [6, 1]]
What would this look like in Javascript?
I want to sort a list filled with lists by their 0th index.
In Python, this would look like
l = [[1, 0], [5, 1], [6, 2], [3, 2]
l.sort(key=lambda x: x[0])
#l is now [[1, 0], [3, 2], [5, 1], [6, 1]]
What would this look like in Javascript?
Array sort method does the same job:
const arr = [[1, 0], [5, 1], [6, 2], [3, 2]];
arr.sort((a,b) => a[0] - b[0]);
console.log(arr);
You could create a function which takes a function fro getting the value for sorting.
let l = [[1, 0], [5, 1], [6, 2], [3, 2]],
fn = x => x[0],
sort = fn => (a, b) => fn(a) - fn(b);
l.sort(sort(fn));
l.forEach(a => console.log(...a));