Sorting and tagging values in objects Wherever the biggest number is always use the 'big' value

If another number is equal to that number, use the 'big' value

Set the next number in line to 'medium'

If another number is equal to that number, use the 'medium' value

Set the next number in line to 'small'

If another number is equal to that number, use the 'small' value

entries: any { "FirstNames": 6, "Names": 8, "": 6, "Locations": 3, "Others": 2};

let sorted = this.sortByValues(this.entries);

var obj = {};
for (var i = 0; i &lt;= sorted.length; i++) {
    if (Object.keys(obj).length === 0 &amp;&amp; obj.constructor === Object)
        obj[sorted[i]] = "big";
    else if (this.entries[sorted[i]] == this.entries[sorted[i-1]] &amp;&amp; obj[sorted[i-1]] == "big")
        obj[sorted[i]] = obj[sorted[i-1]];
    else if(this.entries[sorted[i]] &lt; this.entries[sorted[i-1]] &amp;&amp; obj[sorted[i-1]] == "big")
        obj[sorted[i]] = "medium";
    else if(this.entries[sorted[i]] == this.entries[sorted[i-1]] &amp;&amp; obj[sorted[i-1]] == "medium")
        obj[sorted[i]] = obj[sorted[i-1]];
    else if(this.entries[sorted[i]] &lt; this.entries[sorted[i-1]] &amp;&amp; obj[sorted[i-1]] == "medium")
        obj[sorted[i]] = "small";
    else if(this.entries[sorted[i]] == this.entries[sorted[i-1]] &amp;&amp; obj[sorted[i-1]] == "small")
        obj[sorted[i]] = obj[sorted[i-1]];
    else
        obj[sorted[i]] = obj[sorted[i-1]];
}

sortByValues(list : any)
{
    return Object.keys(list).sort(function(a,b){return list[a]-list[b]}).reverse());
}
Only two values of the sorted array are needed to decide the resultant tags, so the algo is:


scan all entries and find the maximum and pre-maximum. 
scan all entries and tag them accordingly in a new object


Tests show 10x speed-up, but of course it's noticeable only on large number of repetitions.

tagValues(data : any) {
    let keys = Object.keys(data);
    let len = keys.length;
    let max = -Infinity, max2 = -Infinity;
    for (let i = 0; i &lt; len; i++) {
        let value = data[keys[i]];
        if (value &gt; max) {
            max2 = max;
            max = value;
        }
    }
    let tagged = {};
    for (let i = 0; i &lt; len; i++) {
        let key = keys[i];
        let value = data[key];
        tagged[key] = value === max ? 'big' : value === max2 ? 'medium' : 'small';
    }
    return tagged;
}


P.S. As for sortByValues, instead of reversing an array after sorting it, invert the comparison.