Skip to content

Instantly share code, notes, and snippets.

@danyaljj
Created July 16, 2015 23:03
Show Gist options
  • Save danyaljj/59e225db86b29c6762c1 to your computer and use it in GitHub Desktop.
Save danyaljj/59e225db86b29c6762c1 to your computer and use it in GitHub Desktop.
Sort map by its keys and return another map in Javascript
sortMapByValue(map) {
var tupleArray = [];
for (var key in map) tupleArray.push([key, map[key]]);
tupleArray.sort(function (a, b) {
return b[1] - a[1]
});
var sortedMap = {};
tupleArray.forEach(function (el) {
sortedMap[el[0]] = el[1]
});
return sortedMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment