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...

895,868 Responses

  1. Polly表示:

    20 Things Only The Most Devoted Birth Injury Lawyers Fans
    Know birth injury law firms; Polly,

  2. Guide To Marc Jacobs Hand Bags: The Intermediate Guide To Marc Jacobs Hand Bags Marc jacobs hand bag

  3. penipu表示:

    Howdy would you mind stating which blog platform you’re using?
    I’m looking to start my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most blogs and I’m looking for
    something completely unique. P.S My apologies for
    getting off-topic but I had to ask!

  4. I love looking through an article that can make men and women think.
    Also, thanks for permitting me to comment!

  5. Ten Things You Learned In Kindergarden Which Will Aid You In Obtaining Mesothelioma Attorney Asbestos Lawyer

  6. RandallTaulp表示:

    buy lisinopril without prescription: lisinopril 15 mg – cost of generic lisinopril

  7. Tia表示:

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

  8. RandallTaulp表示:

    nolvadex pills: lexapro and tamoxifen – tamoxifen rash

  9. Ronaldpsymn表示:

    https://lisinopril.network/# cost of lisinopril 5 mg

  10. Davidtes表示:

    buy cytotec online Cytotec 200mcg price Misoprostol 200 mg buy online

  11. RandallTaulp表示:

    nolvadex steroids: dcis tamoxifen – tamoxifen citrate

  12. Sylvia表示:

    What’s The Reason You’re Failing At Door Fitters Leeds glass replacement leeds
    (Sylvia)

  13. Davidtes表示:

    buy misoprostol over the counter buy cytotec over the counter Cytotec 200mcg price

  14. WilliamRhype表示:

    http://finasteride.store/# order propecia without a prescription

  15. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy ciprofloxacin

  16. WilliamRhype表示:

    https://finasteride.store/# cost generic propecia

  17. Windows表示:

    9 Signs You’re An Expert Secondary Double Glazing Near Me Expert Windows

  18. Davidtes表示:

    hysterectomy after breast cancer tamoxifen tamoxifen hip pain tamoxifen moa

  19. It’s The Complete Cheat Sheet For Search Engine Optimisation Agency
    search optimization

  20. RandallTaulp表示:

    lisinopril tab 5 mg price: price of lisinopril 5mg – lisinopril pills for sale

  21. Ronaldpsymn表示:

    http://cytotec.club/# purchase cytotec

  22. Davidtes表示:

    lisinopril tablets india lisinopril brand name australia purchase lisinopril 40 mg

  23. Tier 2 seo表示:

    Five Killer Quora Answers On Tier 2 Seo Company Tier 2 seo

  24. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# cipro pharmacy

  25. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 7.5 mg

  26. Ronaldpsymn表示:

    https://finasteride.store/# cost of generic propecia online

  27. WilliamRhype表示:

    http://lisinopril.network/# zestril 20 mg tablet

  28. WilliamRhype表示:

    https://lisinopril.network/# cost of lisinopril in canada

  29. gameqgo88com表示:

    Hi! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m undoubtedly enjoying your blog and look forward to new posts.

  30. Nancy表示:

    Guide To Backlink Builder Software: The Intermediate Guide
    In Backlink Builder Software backlink builder software (Nancy)

發佈回覆給「Davidtes」的留言 取消回覆

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