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,409,522 Responses

  1. Penny表示:

    %%

    My web site volvo v70 key, Penny,

  2. Mittie表示:

    %%

    Visit my homepage :: diagnostic assessment mental health (Mittie)

  3. I’d like to find out more? I’d love to find out some additional information.

  4. Celsa表示:

    double glazing repair basingstoke (Celsa)

  5. I have read so many articles about the blogger lovers except
    this article is actually a fastidious post,
    keep it up.

    Here is my page: cosplay costumes women

  6. 420.Bio表示:

    %%

    My site … Psychiatry-Uk (420.Bio)

  7. Hello there! This is my first visit to your blog!
    We are a team of volunteers and starting a new initiative in a
    community in the same niche. Your blog provided us
    valuable information to work on. You have done a outstanding job!

  8. nearest表示:

    %%

    Also visit my website :: nearest

  9. TimothyBrumb表示:

    Buy Vardenafil 20mg online: Generic Levitra 20mg – Buy Vardenafil 20mg

  10. Good article. I definitely love this website. Stick with
    it!

  11. click here表示:

    Hello, I log on to your new stuff on a regular basis.
    Your humoristic style is awesome, keep doing what
    you’re doing!

  12. Having read this I thought it was extremely enlightening.

    I appreciate you spending some time and energy to put this informative article together.
    I once again find myself personally spending a significant amount of time both reading and commenting.
    But so what, it was still worth it!

  13. If you’re looking to securely manage your transactions, it’s essential to buy a verified
    Cash App account. A verified account ensures credibility and trustworthiness,
    providing peace of mind when making payments or receiving funds.
    With a verified account, you can confidently transact without
    worrying about potential issues. Don’t compromise on security, invest in a verified
    Cash App account today!

  14. program表示:

    %%

    my website – program

  15. Layla表示:

    %%

    Feel free to visit my blog: vauxhall insignia key fob; Layla,

  16. become A rep表示:

    %%

    Also visit my blog :: become A rep

  17. Gary Matthaei表示:

    Nicely put, Appreciate it.

    Feel free to surf to my site … https://gayblackboysblog.com

  18. Anton表示:

    %%

    Also visit my web blog :: repair to double glazed windows (Anton)

  19. %%

    Also visit my web site :: suzuki key replacement uk – http://www.wy2bp2kp1dwvfq1j.com

  20. Hello to every body, it’s my first visit of this weblog; this
    weblog carries remarkable and really excellent stuff for visitors.

發佈留言

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