2014-02-25 79 views
1

我是Jquery和Javascript的新手。有人可以幫助我用jquery排序根據發生次數(count)排列。我嘗試過各種排序方法,但都沒有工作。按Javascript數組中排序的次數(count)排序

我在Javascript中的陣列是

allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"] 

// here 2 is printed four times, 6 is printed thrice, and 4 is printed twice 

我需要這樣

newTypesArray = ["2","6","4"] 

我試圖

輸出
function array_count_values(e) { 
var t = {}, n = "", 
    r = ""; 
var i = function (e) { 
    var t = typeof e; 
    t = t.toLowerCase(); 
    if (t === "object") { 
     t = "array" 
    } 
    return t 
}; 
var s = function (e) { 
    switch (typeof e) { 
    case "number": 
     if (Math.floor(e) !== e) { 
      return 
     }; 
    case "string": 
     if (e in this && this.hasOwnProperty(e)) { 
      ++this[e] 
     } else { 
      this[e] = 1 
     } 
    } 
}; 
r = i(e); 
if (r === "array") { 
    for (n in e) { 
     if (e.hasOwnProperty(n)) { 
      s.call(t, e[n]) 
     } 
    } 
} 
return t 
} 
6: 3 
} 

輸出是 {4: 2, 2: 6, 6:3}

+4

你能告訴我們你的代碼這麼遠? – elclanrs

+0

這些不同的方法是什麼?他們怎麼不工作? –

+1

我希望下面的信息可能幫助你達到同樣的.. http://stackoverflow.com/questions/19464440/jquery-javascript-sort-array-by-highest-count – JayP

回答

1

只是增加了我的想法,以及(有點晚

var allTypesArray = ["4", "4", "2", "2", "2", "6", "2", "6", "6"]; 
var map = allTypesArray.reduce(function (p, c) { 
    p[c] = (p[c] || 0) + 1; 
    return p; 
}, {}); 
var newTypesArray = Object.keys(map).sort(function (a, b) { 
    return map[a] < map[b]; 
}); 
+0

非常優雅的答案 - 但是,正如我下面注意到的,它似乎不適用於Safari 10. – lnNoam

+0

它是否會在控制檯中產生錯誤的結果或某些錯誤? –

+0

嗨。結果是錯誤的。它返回:'[「2」,「4」,「6」]'。 – lnNoam

5

我不認爲在一個步驟中有直接的解決方案,當然這不僅僅是一種排序(排序不會刪除元素)。一種方法是建立一個存儲計數對象的中間圖:

var allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"]; 
var s = allTypesArray.reduce(function(m,v){ 
    m[v] = (m[v]||0)+1; return m; 
}, {}); // builds {2: 4, 4: 2, 6: 3} 
var a = []; 
for (k in s) a.push({k:k,n:s[k]}); 
// now we have [{"k":"2","n":4},{"k":"4","n":2},{"k":"6","n":3}] 
a.sort(function(a,b){ return b.n-a.n }); 
a = a.map(function(a) { return a.k }); 

請注意,在這裏不需要jQuery。當你不操縱DOM時,你很少需要它。

+1

FWIW你可以保存幾個字符返回m [v] = ++ m [v] || 0,m' – elclanrs

+0

謝謝。你救了我。 :) – Chopra

+1

@elclanrs它看起來像我試圖打高爾夫? ^^ –

0

我不認爲jQuery是需要在這裏。

這個問題已經有好幾個很好的答案了,但是我發現可靠性在某些瀏覽器(即Safari 10--儘管可能有其他的瀏覽器)中成爲問題。

一個有點難看,但看似可靠,方法來解決這個問題如下:

function uniqueCountPreserve(inputArray){ 
    //Sorts the input array by the number of time 
    //each element appears (largest to smallest) 

    //Count the number of times each item 
    //in the array occurs and save the counts to an object 
    var arrayItemCounts = {}; 
    for (var i in inputArray){ 
     if (!(arrayItemCounts.hasOwnProperty(inputArray[i]))){ 
      arrayItemCounts[inputArray[i]] = 1 
     } else { 
      arrayItemCounts[inputArray[i]] += 1 
     } 
    } 

    //Sort the keys by value (smallest to largest) 
    //please see Markus R's answer at: http://stackoverflow.com/a/16794116/4898004 
    var keysByCount = Object.keys(arrayItemCounts).sort(function(a, b){ 
     return arrayItemCounts[a]-arrayItemCounts[b]; 
    }); 

    //Reverse the Array and Return 
    return(keysByCount.reverse()) 
} 

測試

uniqueCountPreserve(allTypesArray) 
//["2", "6", "4"]