JQuery Javascript Sort Array by Highest Count

后端 未结 5 520
清酒与你
清酒与你 2021-01-23 06:54

I have:

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

I need to count by occurrences and sort by highest count. This w

相关标签:
5条回答
  • 2021-01-23 07:23

    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]});
      }
    }
    
    0 讨论(0)
  • 2021-01-23 07:27
    $array = array("ABAB", "ABAB", "ABAB", "CDCD", "EFEF", "EFEF");
       $array1=array_count_values($array);
    
        arsort($array1);
    
    
        foreach($array1 as $x=>$x_value)
        {
            echo "Key=" . $x . ", Value=" . $x_value;
            echo "<br>";
        }
    
    0 讨论(0)
  • 2021-01-23 07:28

    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.

    0 讨论(0)
  • 2021-01-23 07:34

    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);
    
    0 讨论(0)
  • 2021-01-23 07:43

    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 ] ]
    
    0 讨论(0)
提交回复
热议问题