How to sort a map by value in ascending order with Node.js

1 Answer

0 votes
function print(mp) {
  for (let [key, value] of mp) {
      console.log(key, value);            
  }
}
 
let mp = new Map([
  ['JavaScript', 4],
  ['TypeScript', 3],
  ['C++',1],
  ['PHP',5],
  ['Node.js', 2],
]);
 
 
mp = new Map([...mp].sort((a, b) => a[1] - b[1]));
 
print(mp);
 
 
   
   
   
   
/*
run:
   
C++ 1
Node.js 2
TypeScript 3
JavaScript 4
PHP 5
   
*/

 



answered Feb 23, 2022 by avibootz

Related questions

1 answer 196 views
1 answer 167 views
1 answer 170 views
1 answer 102 views
2 answers 190 views
...