Skip to content Skip to sidebar Skip to footer

Jquery Javascript Sort Array By Highest Count

I have: myArray = ['ABAB', 'ABAB', 'ABAB', 'CDCD', 'EFEF', 'EFEF'] I need to count by occurrences and sort by highest count. This would be the return: ABAB 3 EFEF 2 CDCD 1 Note,

Solution 1:

Step one: build the histogram, as a map element -> its frequency for speed (assumes all elements are strings):

var histogramMap = {};
for(var i=0, len=myArray.length; i<len; i++){
  var key = myArray[i];
  histogramMap[key] = (histogramMap[key] || 0) + 1;
}

Step two: convert to an array of output objects:

var histogram = [];
for(key in histogramMap) histogram.push({key: key, freq: histogramMap[key]});

Step three: sort the histogram

histogram.sort(function(a,b){return b.freq - a.freq})

This also assumes that Object.prototype is not modified. This is a safe assumption to make, and lots (I think) of libraries, including jQuery, make that assumption. But, if you decide to add enumerable properties to Object.prototype, these will be picked up by for..in. If you want to be safe, modify the second step to:

var histogram = [];
for(key in histogramMap){
  if(histogramMap.hasOwnProperty(i)){
    histogram.push({key: key, freq: histogramMap[key]});
  }
}

Solution 2:

I recently created a library with such a function.

var items = {}, sortableItems = [], i, len, element,
    listOfStrings = ["ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF"];

for (i = 0, len = listOfStrings.length; i < len; i += 1) {
    if (items.hasOwnProperty(listOfStrings[i])) {
        items[listOfStrings[i]] += 1;
    } else {
        items[listOfStrings[i]] = 1;
    }
}

for (element in items) {
    if (items.hasOwnProperty(element)) {
        sortableItems.push([element, items[element]]);
    }
}

sortableItems.sort(function(first, second) {
    return second[1] - first[1];
});

console.log(sortableItems);

Output

[ [ 'ABAB', 3 ], [ 'EFEF', 2 ], [ 'CDCD', 1 ] ]

Solution 3:

Try this:

array_elements = ["ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF"];

var result_array = [];

var current = null;
var cnt = 0;
for (var i = 0; i < array_elements.length; i++) {
    if (array_elements[i] != current) {
        if (cnt > 0) {
            result_array.push([current,cnt]);
        }
        current = array_elements[i];
        cnt = 1;
    } else {
        cnt++;
    }
}
if (cnt > 0) {
    result_array.push([current,cnt]);
}

result_array.sort(function(x,y) {return y[1] - x[1]})
alert(result_array);

The result_array will have the result.

Solution 4:

see console in firefox to see the resulting object

http://jsfiddle.net/4dqeK/1/

myArray = ["ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF"];

container={};

 for(var i=0; i < myArray.length;i++){
        var el=myArray[i];
        if( el in container){
            container[el].push(el);
        }
        else{
            container[el]=[];
            container[el].push(el);
        }


    }
    console.log(container);

Solution 5:

$array = array("ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF");
   $array1=array_count_values($array);

    arsort($array1);


    foreach($array1as$x=>$x_value)
    {
        echo"Key=" . $x . ", Value=" . $x_value;
        echo"<br>";
    }

Post a Comment for "Jquery Javascript Sort Array By Highest Count"