JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,245,502 Responses

  1. Mobic Edge表示:

    Appreciate this post. Let me try it out.

  2. BrandenSlomy表示:

    price of prednisone 5mg: ray pharm – otc prednisone cream

  3. Hello, i read your blog occasionally and i own a similar one and i was just wondering if
    you get a lot of spam feedback? If so how do you stop it,
    any plugin or anything you can recommend? I get so much lately it’s driving me mad so any support
    is very much appreciated.

  4. pecah perawan表示:

    Magnificent site. Plenty of useful info here.
    I’m sending it to several friends ans additionally sharing in delicious.
    And naturally, thank you to your effort!

  5. BrandenSlomy表示:

    cheap clomid no prescription: clomid rex pharm – can i buy cheap clomid online

  6. It’s amazing to pay a visit this site and reading the views of all colleagues on the topic of this post,
    while I am also zealous of getting knowledge.

  7. great issues altogether, you simply received a emblem new reader.
    What might you recommend about your submit that you just made a few
    days in the past? Any positive?

  8. Dereketerm表示:

    order amoxicillin no prescription: buy amoxil online – where can i buy amoxicillin over the counter

  9. BrandenSlomy表示:

    buy dapoxetine online: buy dapoxetine online – priligy maxpharm

  10. Dereketerm表示:

    amoxicillin 500 mg: where to buy amoxicillin over the counter – amoxicillin 250 mg price in india

  11. Iyannaobesk表示:

    fortune mouse gratis
    fortune mouse gratis слот

  12. whatsapp表示:

    Right here is the right blog for anybody who hopes to understand this topic.
    You understand a whole lot its almost hard to argue with you
    (not that I really will need to…HaHa). You definitely put a brand new spin on a topic that’s been written about for a
    long time. Excellent stuff, just great!

  13. Blakesmema表示:

    кухни на заказ — Выберите идеальный дизайн кухни, созданной специально для вас.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。