3

Given Map

{
  'i' => 2, 
  'love' => 2, 
  'leetcode' => 1, 
  'coding' => 1
}

I want to,

  • Sort the Map based on value
  • if the value matches, sort by key where the word with the lower alphabetical order comes first.

Expected

{
  "i" => 2, 
  "love" => 2, 
  "coding" => 1,
  'leetcode' => 1, 
}

Solution i have tried

let map = new Map();
map.set("i", 2);
map.set("love", 2);
map.set("coding", 1);
map.set("live", 1);

let output = new Map([...map.entries()].sort((a, b) => {
  //if b[0] and a[0] matches sort by  key stuck here
  return b[0] - a[0];
  
}));

console.log(output);

2
  • Subtraction works with numbers. For strings, compareFunction should be b[0].localeCompare(a[0]) Commented Apr 5, 2019 at 8:07
  • 1
    btw, ...map.entries() is the same as just spreading the map ...map. Commented Apr 5, 2019 at 8:09

2 Answers 2

5

Value is present at index 1 so update the index to 1 for sorting based on value and use String#localeCompare for string comparison.

let map = new Map();
map.set("i", 2);
map.set("love", 2);
map.set("coding", 1);
map.set("live", 1);

// for sorting based on number we need to return the difference
// in case those are equals difference would be 0(falsy value)
// and the or part will be executed and returns(string comaprison)
let output = new Map([...map].sort((a,b) => b[1] - a[1] || a[0].localeCompare(b[0]) ));

console.log(...output);


or with ES6 Destructuring assignment.

let map = new Map();
map.set("i", 2);
map.set("love", 2);
map.set("coding", 1);
map.set("live", 1);

let output = new Map([...map].sort( ([k1, v1], [k2, v2]) => v2 - v1 || k1.localeCompare(k2) ));

console.log(...output);

FYI : To get a basic idea of how Logical operators refer to MDN docs.

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

3 Comments

Can you also please explain it will be great rather than just the answer
@RahulSingh : added some basic explanation
yes those two lines made me accept the ans over @Nina
3

You need to sort the count descending and the string with String#localeCompare ascending.

The first part takes a delta of the two values and while Array#sort is expecting a value

  • smaller than zero for then a is smaller then b, which means both have the right order, like 1 - 2 where you get -1,

  • equal zero, that means bot elements have the same value, like 7 - 7, or

  • greater than zero, which means both elements have to swap, like 5 - 3.

The same result returns localeCompare with strings.

let map = new Map();
map.set("coding", 1);
map.set("love", 2);
map.set("live", 1);
map.set("i", 2);

let output = new Map([...map].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])));

console.log([...output]);

3 Comments

Sort the Map based on value and if value matches sort by key So, b[1] -a[1] || a[0].localeCompare(b[0])
@Nina i guess you missed it , this only compares the string keys not the values and if values matches then you compare keys
Can you also please explain it will be great rather than just the answer

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.