Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
534 B
1
Indexable
Never
function sortbyValue(dataMap) {
  var map = new Map();
  map.set(4, [1, 2, 3]);
  map.set(3, [1, 2, 3, 5, 6]);
  map.set(1, [1, 2, 3, 4]);

  console.log("map: >> " + JSON.stringify([...map.keys()]));
  let indexes = [...map.keys()].sort((a, b) => {
    console.log(">> " + JSON.stringify(a));
    return map.get(b).length - map.get(a).length;
  });
  var mapAsc = new Map();
  indexes.forEach((element) => {
    mapAsc.set(element, map.get(element));
  });

  console.log(mapAsc);
  return JSON.stringify(mapAsc);
}