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,236,261 Responses

  1. The 10 Most Scariest Things About Window Screen Replacement window screen Replacement

  2. Where Will Prada Bag For Men Be 1 Year From Right Now?
    Prada Bags gold

  3. glk-egoza.ru表示:

    17 Reasons Why You Should Be Ignoring Search Engine Optimization search engine optimization website
    (glk-egoza.ru)

  4. WilliamRhype表示:

    http://lisinopril.network/# lisinopril medication

  5. The 3 Greatest Moments In Mesothelioma History Mesothelioma Legal

  6. The 10 Most Scariest Things About 18 Wheeler Wreck Lawyer 18 Wheeler Accident lawyer

  7. 17 Reasons You Shouldn’t Ignore Railroad Injuries Attorneys railroad injuries Lawsuits

  8. Te.Legra.Ph表示:

    You’ll Be Unable To Guess Automated Backlink Builder Software’s Tricks Backlink Builder Software,
    Te.Legra.Ph,

  9. auto表示:

    10 Best Facebook Pages Of All Time About Motor Vehicle Attorneys auto

  10. The People Who Are Closest To Combo Washing Dryer Share Some Big Secrets combo washing dryer

  11. WilliamRhype表示:

    http://ciprofloxacin.tech/# ciprofloxacin generic price

  12. I’m extremely impressed with your writing skills as neatly as with the
    layout for your blog. Is that this a paid theme or did you customize it your self?

    Either way stay up the excellent high quality writing, it’s uncommon to look a great
    blog like this one today..

  13. RandallTaulp表示:

    buy cytotec online fast delivery: Misoprostol 200 mg buy online – cytotec abortion pill

  14. WilliamRhype表示:

    http://cytotec.club/# Abortion pills online

  15. Find Out What Folding Mobility Scooters The Celebs Are Using foldable mobility scooter lightweight

  16. What’s The Job Market For Cerebral Palsy Attorney Professionals?

    Cerebral Palsy Law Firms

  17. 15 Things You’re Not Sure Of About Avon Become A Rep Become An avon Rep online

  18. Ronaldpsymn表示:

    http://cytotec.club/# buy cytotec online fast delivery

  19. Ranker X表示:

    What You Need To Do On This Backlink Generator Software Ranker X

  20. WilliamRhype表示:

    https://nolvadex.life/# effexor and tamoxifen

  21. Davidtes表示:

    buy lisinopril online no prescription india how much is lisinopril zestril 30 mg

  22. 5 Common Myths About Truck Accident Attorney For Hire You Should Stay Clear Of Truck Accident Law Firms

  23. Window Repairs Tips To Relax Your Daily Life Window Repairs Trick That Everybody Should Know Window repairs

  24. Davidtes表示:

    buy cheap propecia online cost generic propecia no prescription buying cheap propecia

  25. 10 Tell-Tale Signals You Should Know To Know Before You Buy Double.Glazing Near Me double glazing windows

  26. BITUY表示:

    My spouse and I absolutely love your blog and find almost all of
    your post’s to be exactly what I’m looking for.

    Do you offer guest writers to write content for you personally?
    I wouldn’t mind publishing a post or elaborating on some of the subjects
    you write regarding here. Again, awesome weblog!

  27. Ronaldpsymn表示:

    http://cytotec.club/# п»їcytotec pills online

  28. RandallTaulp表示:

    lisinopril 5 mg india price: lisinopril coupon – zestril 10 mg price

  29. 9 Things Your Parents Teach You About Double Glazed Windows Near Me
    double glazed windows Near me

發佈留言

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