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,372,985 Responses

  1. Pedroereva表示:

    mexico drug stores pharmacies: Online Pharmacies in Mexico – best online pharmacies in mexico

  2. Rickysem表示:

    indian pharmacy online Cheapest online pharmacy online shopping pharmacy india

  3. The Leeds Door And Window Success Story You’ll Never Imagine French Door Repairs Leeds

  4. pharma表示:

    This piece of writing will help the internet users for building up new webpage or even a
    weblog from start to end.

  5. JustinCit表示:

    Online medicine home delivery https://indiaph24.store/# best india pharmacy
    top 10 pharmacies in india

  6. Who’s The Most Renowned Expert On Fridge Freezers?
    Integrated fridge freezer for sale

  7. replacements表示:

    10 Beautiful Images Of Replacing Lost Car Key replacements

  8. Marcelabelp表示:

    http://mexicoph24.life/# medication from mexico pharmacy

  9. JustinCit表示:

    world pharmacy india https://indiaph24.store/# india pharmacy mail order
    buy prescription drugs from india

  10. Rickysem表示:

    buying prescription drugs in mexico online Online Pharmacies in Mexico mexican rx online

  11. Ten Tommy Hilfiger Crossbody Handbags That Really Change Your Life
    Tommy Hilfiger Crossbody Handbags

  12. What To Look For To Determine If You’re Prepared To Portable
    Mobility Scooters For Sale luggie mobility scooter for sale

  13. JustinCit表示:

    reputable indian pharmacies https://indiaph24.store/# buy medicines online in india
    Online medicine order

  14. 15 Of The Best Pinterest Boards Of All Time About Window Glass Replacement Window glass Replacement Near me

  15. 10 Things Everyone Hates About Marc Jacobs Bags marc jacobs snapshot bag sale

  16. SEO UK Company Tools To Help You Manage Your Daily
    Lifethe One SEO UK Company Trick That Every Person Must
    Learn Seo uk Company

  17. What Are The Reasons You Should Be Focusing On Improving Vauxhall Astra Key Replacement Vauxhall Spare Key

  18. The 10 Most Terrifying Things About Lightweight Automatic Folding Mobility Scooters
    Automatic Folding Mobility scooters

  19. Rickysem表示:

    legal to buy prescription drugs from canada Prescription Drugs from Canada canadian pharmacy reviews

  20. See What Double Glazing Repairs Near Me Tricks The Celebs Are Using double glazing repairs Near me

  21. For the reason that the admin of this web site is working, no uncertainty very quickly it
    will be well-known, due to its quality contents. https://Reallivesocial.com/story2442040/tres-amigos-outfitters

  22. JustinCit表示:

    top 10 pharmacies in india https://indiaph24.store/# online pharmacy india
    top 10 online pharmacy in india

  23. dnpaint.co.kr表示:

    A Look Into The Future What’s In The Pipeline? Motor Vehicle Lawsuit Industry Look
    Like In 10 Years? motor vehicle Accident lawsuits – dnpaint.co.kr

  24. 15 Things You Don’t Know About Mini Cooper Car Key Replacement
    mini key Fob replacement

  25. Hi there! This is kind of off topic but I need some advice from an established
    blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about making my own but I’m not sure where to begin. Do you have any ideas or suggestions?
    Appreciate it

  26. Pedroereva表示:

    mexico drug stores pharmacies: cheapest mexico drugs – mexican pharmacy

  27. Michaelcew表示:

    http://mexicoph24.life/# pharmacies in mexico that ship to usa

  28. hand bag表示:

    20 Insightful Quotes On Designer Handbags For Sale hand bag

發佈留言

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