SHARE
TWEET

sorting JS

MartinGeorgiev Jul 10th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let arrInput = [2,2,5,5,3,3,0,0,1,1,1,1,1,4,4,4,4,4,6];
  2. let map = new Map();
  3. for(let element of arrInput){
  4.     if (map.has(element) === false) {
  5.             map.set(element, 1)
  6.         }else {
  7.              let current = map.get(element) + 1;
  8.              map.set(element, current);
  9.         }
  10. }
  11. var mapAsc = new Map([...map.entries()].sort());
  12. let sorted = [...mapAsc].sort((a, b) => a[1] - b[1]);
  13.     for (var [key, value] of sorted) {
  14.         console.log(`${key} -> ${value} times`)  
  15.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top