0

How can I sort a list of lists by the second element in the sublist? I feel like this should work but it isn't:

var x = [[1, 'c'], [3, 'a'], [5, 'b']];
var sorted = x.sort(function(a, b) {
  return a[1] > b[1];
});
console.log(x);
# [ [ 1, 'c' ], [ 3, 'a' ], [ 5, 'b' ] ]
console.log(sorted);
# [ [ 1, 'c' ], [ 3, 'a' ], [ 5, 'b' ] ]
Johnny Metz
  • 4,802
  • 11
  • 60
  • 123

0 Answers0