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,435,157 Responses

  1. No matter if some one searches for his necessary thing,
    so he/she wishes to be available that in detail, thus
    that thing is maintained over here.

  2. Jina表示:

    Howdy! Thіs blog post could not Ьe ԝritten any Ƅetter!
    Looking at tһis post reminds me of my previouѕ roommate!
    He always kеpt preaching abⲟut this. I am going tօ ѕend tһis
    post to him. Pretty sսre he’s going t᧐ haѵe a greɑt
    read. I appreciate you fоr sharing!

    Ꮇy web blog lotto (Jina)

  3. mesothelioma lawyers houston texas (http://worksale.nnov.org)

  4. RichardLak表示:

    Kamagra tablets Kamagra 100mg price cheap kamagra

  5. Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point.
    You clearly know what youre talking about, why throw away your intelligence
    on just posting videos to your blog when you could be giving us something
    enlightening to read?

  6. Scooters表示:

    %%

    Here is my page Scooters

  7. Away.Vk.Com表示:

    %%

    Stop by my web page – Certified Online Jobs Work From Home (Away.Vk.Com)

  8. Orval表示:

    %%

    Also visit my web page – avon shop with my rep; Orval,

  9. Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m trying
    to find things to improve my site!I suppose its ok to use some of your ideas!!

  10. I’m very happy to uncover this page. I want to
    to thank you for your time just for this wonderful read!!
    I definitely savored every part of it and i also have you
    book-marked to look at new things in your website.

  11. I am no longer positive the place you’re getting your info, but good topic.

    I must spend a while finding out much more or figuring out more.
    Thanks for magnificent information I used to be on the lookout for this information for my
    mission.

  12. %%

    Also visit my web site – Auto Locksmithing

  13. Please let me know if you’re looking for a writer for your site.
    You have some really good posts and I believe I would be a good asset.
    If you ever want to take some of the load
    off, I’d love to write some material for your blog in exchange for a
    link back to mine. Please shoot me an e-mail if interested.
    Thanks!

  14. Helpful information. Fortunate me I discovered your website by chance, and
    I’m surprised why this twist of fate did not happened earlier!

    I bookmarked it.

  15. phlmsexx表示:

    Hi, after reading this awesome paragraph i am too delighted to share my experience here with colleagues.

  16. asbestos表示:

    %%

    Here is my site – asbestos

  17. upvc casement windows bishops stortford (yogaasanas.science)

  18. %%

    Feel free to surf to my web page; door Fitters Near me

  19. Kristine表示:

    %%

    Feel free to visit my webpage: buy second hand mobility scooter [Kristine]

  20. Very nice article, totally what I was looking for.

  21. Mavis表示:

    %%

    Also visit my site; representative sales [Mavis]

  22. Felipa表示:

    %%

    my web blog … asbestos compensation (Felipa)

發佈留言

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