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,155,730 Responses

  1. Jackpot Slots表示:

    10 Basics To Know Slot Payouts You Didn’t Learn In School Jackpot Slots

  2. Raina表示:

    This History Behind Truck Accident Claim Can Haunt You Forever!
    truck accident lawyers (Raina)

  3. Drug prescribing information. Short-Term Effects.
    can you get zyban pill
    Everything what you want to know about drug. Read information here.

  4. Window表示:

    5 Killer Quora Answers To Window Glass Replacement Near
    Me Window

  5. Medicament prescribing information. Drug Class.
    can i order cheap azathioprine
    Actual information about drug. Read here.

  6. 15 Things You’ve Never Known About Window Repair Near double glazed window repairs near me
    (animeportal.cl)

  7. 10 Tell-Tale Symptoms You Need To Find A New Mesothelioma Lawyer mesothelioma claims

  8. 5 Laws That’ll Help The Best Buy Mobility Scooters
    Industry The Best Mobility Scooter

  9. Medicines information. Cautions.
    where to buy tamoxifen without insurance
    All trends of meds. Read information here.

  10. The 10 Scariest Things About Slot Promotions Slot
    Promotions (21.farcaleniom.Com)

  11. Fun Slots表示:

    The Three Greatest Moments In High Limit Slots History Fun Slots

  12. Emma表示:

    lesbian porn

  13. This is nicely said. .

  14. Witam serdecznie społeczność forum i na początek chciałbym was zapytać o to jakie portale internetowe goszczą najczęściej w waszych przeglądarkach Jakie są wasze preferencje w tej kwestiii ?

    Ja np. odwiedzam agregator newsów z różnych portali którego jestem skromnym administratorem co go wyróżnia od innych witryn tego typu ? sami sprawdzcie i oceńcie 🙂

  15. The 10 Most Scariest Things About Uk Online Shopping Sites For Electronics uk online shopping Sites for electronics

  16. Unquestionably believe that which you said. Your favorite justification seemed
    to be on the net the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries
    that they plainly don’t know about. You managed to hit the
    nail upon the top and defined out the whole thing without having side-effects , people can take
    a signal. Will likely be back to get more. Thanks

  17. lawsuits表示:

    15 Presents For The Workers Compensation Attorneys Lover In Your Life
    lawsuits

  18. Artrecord.Kr表示:

    17 Reasons To Not Ignore Double Glazing Near Me Double
    Glazed Windows Repairs [Artrecord.Kr]

  19. Learn About Upvc External Doors When You Work From The Comfort Of Your Home replacing a upvc door panel

  20. Hey I know this is off topic but I was wondering if you
    knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some
    time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog
    and I look forward to your new updates.

  21. звуковое оборудование для конференц зала звуковое оборудование для конференц зала .

  22. Augusta表示:

    What Are The Biggest “Myths” About Slots For Fun Could Actually Be Accurate casino slot tournaments (Augusta)

  23. Thanks for yοur personal marvelous posting!
    І aсtually enjoyed reading іt, you might bе a ɡreat author.Ӏ
    will ensure that I bookmark үoᥙr blog ɑnd will ⲟften сome back somеtime soon.
    Ӏ want tⲟ encourage үoս continue уοur great writing, hɑᴠе a nice evening!

  24. rack server表示:

    Appreciate the recommendation. Let me try it out.

  25. Mobile Slots表示:

    The Slot Reviews Case Study You’ll Never Forget Mobile Slots

  26. The Repair Double Glazing Window Success Story You’ll Never
    Believe http://www.257634.xyz

發佈留言

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