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,481,113 Responses

  1. CurtisIncix表示:

    pinup: pinup kazi – pinup kazi

  2. Kate Goodman表示:

    Thanks for the great content. More at indian catering london

  3. Hey there! I’m at work surfing around your blog from my new iphone 4!
    Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the outstanding work!

  4. William Hines表示:

    Protect your house from undesirable invaders with the lend a hand of Pest Control ‘s first-rate pest handle expertise

  5. I liked this article. For additional info, visit transporte de mochilas camino de Santiago

  6. Porn Sex表示:

    We’re a group of volunteers and opening a new scheme in our community.
    Your site provided us with valuable info to
    work on. You’ve done a formidable job and our whole community will be grateful to you.

  7. Pills information for patients. Effects of Drug Abuse.
    get cheap elavil pill
    Actual information about drug. Get now.

  8. DarioTob表示:

    pin up казино: пинап казино – pin up казино

  9. CarltonAnall表示:

    Качественный асфальт выдерживает любые погодные условия и нагрузки, что делает его идеальным для парковок, дорожек и дворов – асфальтирование площадок

  10. slot表示:

    online casino

  11. Sophia Bryan表示:

    For anyone interested in historic home renovations in Chicago Revive Renovations home remodeling

  12. It’s actually a great and helpful piece of info. I
    am satisfied that you shared this helpful information with us.
    Please keep us informed like this. Thanks for sharing.

    my web-site; dvd distributors

  13. It’s enormous that you are getting thoughts from this post as well as from our
    argument made at this place.

  14. Jamesdiure表示:

    vavada-kazi.ru: вавада – вавада

  15. Hi to all, how is the whole thing, I think every one is getting more from this website, and your
    views are pleasant designed for new visitors.

  16. I am not sure the place you are getting your information, but great topic.
    I needs to spend some time studying more or working out more.
    Thanks for excellent info I used to be in search of this information for
    my mission.

  17. google xg表示:

    Aw, this was a very nice post. Taking a few minutes and actual effort to make a
    very good article… but what can I say…
    I hesitate a lot and don’t manage to get nearly anything done.

  18. DarioTob表示:

    vavada kazi: вавада казино онлайн – vavada kazi

  19. HenrydeT表示:

    казино вавада вавада казино вавада онлайн казино

  20. Jamesdiure表示:

    pinup: pinup-kazi.kz – pin up казино

發佈留言

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