1

I have this array :

[ [ 1, 'a' ], [ 2, 'b' ], [ 1, 'd' ], [ 9, 'e' ], [ 1, 'f' ], [ 11, 'g' ], [ 9, 'h' ], [ 3, 'i' ] ]

and I would like to have :

[ [ 11, 'g' ], [ 9, 'e' ], [ 9, 'h' ], [ 3, 'i' ], [ 2, 'b' ], [ 1, 'a' ], [ 1, 'd' ], [ 1, 'f' ] ]

How can I do that with javascript please ?

I tried sort(), I also tried with sort(compare) with :

function compare(x, y) {
  return x - y;
}

4 Answers 4

5

You can use .sort() with Array Destructuring like this:

function compare([a], [b]) {
  return b - a;
}

Demo:

let a = [ [ 1, 'a' ], [ 2, 'b' ], [ 1, 'd' ], [ 9, 'e' ], [ 1, 'f' ], [ 11, 'g' ], [ 9, 'h' ], [ 3, 'i' ] ];

a.sort(compare);

function compare([a], [b]) {
  return b - a;
}

console.log(a);
.as-console-wrapper { max-height: 100% !important; top: 0; }


In case first elements match, you can sort based on 2nd element as well:

function compare([a, c], [b, d]) {
  return (b - a) || c.localeCompare(d)
}

Demo:

let a = [ [ 2, 'b' ], [ 1, 'd' ], [ 9, 'e' ], [ 1, 'f' ], [ 11, 'g' ], [ 9, 'h' ], [ 3, 'i' ], [ 1, 'a' ] ];

a.sort(compare);

function compare([a, c], [b, d]) {
  return (b - a) || c.localeCompare(d);
}

console.log(a);
.as-console-wrapper { max-height: 100% !important; top: 0 }

Sign up to request clarification or add additional context in comments.

6 Comments

c > d I would avoid such sorting. It might work here but sorting it's important that all three states can be captured. < = & > doing just the two will work most of the time, but can fail.
@Keith What do you suggest instead of >?
Use the - This will give you all 3 states.
@Keith I've already tried with - but - doesn't seems to work with string data type. See this Fiddle
Yes, for string localeCompare is better -> (b - a) || c.localeCompare(d);
|
4

You need to compare the first element in the nested array since you want to sort based on that number.

function compare(x, y) {
  return y[0] - x[0];
}

var data = [
  [1, 'a'],
  [2, 'b'],
  [1, 'd'],
  [9, 'e'],
  [1, 'f'],
  [11, 'g'],
  [9, 'h'],
  [3, 'i']
];

function compare(x, y) {
  return y[0] - x[0];
}

data.sort(compare);

console.log(data);


In case you want to sort based on second element(secondary sorting in case the first element is same) then use String#localeCompare method for comparing.

function compare(x, y) {
  return y[0] - x[0] || x[1].localeCompare(y[0]);
}

var data = [
  [2, 'b'],
  [1, 'd'],
  [9, 'e'],
  [1, 'f'],
  [1, 'a'],
  [11, 'g'],
  [9, 'h'],
  [3, 'i']
];

function compare(x, y) {
  return (y[0] - x[0]) || x[1].localeCompare(y[1]);
}

data.sort(compare);

console.log(data);

1 Comment

Second example, Your code shows && that's incorrect, but your code snippet is using || that's correct. Had me baffled for a while, how come his code snippet is working. :)
1

Compare the elements based on their first element, which is the number.

var a = [ [ 1, 'a' ], [ 2, 'b' ], [ 1, 'd' ], [ 9, 'e' ], [ 1, 'f' ], [ 11, 'g' ], [ 9, 'h' ], [ 3, 'i' ] ];

a = a.sort((a,b) => {
    return b[0] - a[0]
});

console.log(a)

Comments

1

Use sort to compare first element and if first element is same then compare the second element.

arr.sort( (a,b) => (b[0] - a[0]) || (b[1] - a[1]) )

Demo

var arr = [ [ 1, 'a' ], [ 2, 'b' ], [ 1, 'd' ], [ 9, 'e' ], [ 1, 'f' ], [ 11, 'g' ], [ 9, 'h' ], [ 3, 'i' ] ];

arr.sort( (a,b) => (b[0] - a[0]) || (b[1] - a[1]) );

console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.