Ok, I think I got what you meant...
O(NlogN) solution, if there are N items in _map
var _map = {
'severity-normal': 0,
'severity-minimal': 0,
'severity-moderate': 0,
'severity-severe': 0,
'severity-highly-severe': 0
};
var list = [];
$.each(_map, function(key, value) {
list.push({key:key, value:value});
});
list.sort(function(a,b) { return b.value - a.value; } );
for(var i=0; i<3 && i<list.length; i++) {
alert(i + ': ' + list[i].value);
}
O(K*N) solution, if you want to get the top K items and there are N items
var _map = {
'severity-normal': 0,
'severity-minimal': 0,
'severity-moderate': 0,
'severity-severe': 0,
'severity-highly-severe': 0
};
function getTopElements(map, k) {
var lastMaximum = Infinity, currentMaximum;
var outputList = [];
while(outputList.length < k) {
currentMaximum = -Infinity;
for(key in map) {
if(map[key] > currentMaximum && map[key] < lastMaximum) {
currentMaximum = map[key];
}
}
for(key in map) {
if(map[key] == currentMaximum) {
outputList.push(map[key]);
}
if(outputList.length >= k) break;
}
lastMaximum = currentMaximum;
}
return outputList;
}
var list = getTopElements(map,3);
for(var i=0; i<list.length; i++) {
alert(i + ': ' + list[i].value);
}