Created
July 16, 2015 23:03
-
-
Save danyaljj/59e225db86b29c6762c1 to your computer and use it in GitHub Desktop.
Sort map by its keys and return another map in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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