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,234,231 Responses

  1. JustinCit表示:

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

  2. Pedroereva表示:

    mexican drugstore online: mexico pharmacy – medication from mexico pharmacy

  3. Thanks , I have recently been searching for info about this topic for ages and yours is the best I have found out till now.
    But, what concerning the conclusion? Are you certain in regards to the supply?

  4. Rickysem表示:

    canada pharmacy online Large Selection of Medications from Canada safe canadian pharmacy

  5. 11 Strategies To Completely Redesign Your 12kg
    Washing Machines washing Machine 12Kg price

  6. attorney表示:

    What’s The Current Job Market For Injury Attorney Professionals Like?
    attorney

  7. Michaelcew表示:

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

  8. Rickysem表示:

    is canadian pharmacy legit best online canadian pharmacy legal canadian pharmacy online

  9. Rickysem表示:

    canada pharmacy canadian compounding pharmacy canadian pharmacy 24h com

  10. Ten Treatments Of Adhds That Really Change Your Life
    treatment for Adhd in adults uk

  11. Speak “Yes” To These 5 Car Boot Lock Repair Tips central locking Repair near me

  12. JustinCit表示:

    india pharmacy http://indiaph24.store/# mail order pharmacy india
    top 10 online pharmacy in india

  13. Why Window Companies Leeds Is Your Next Big Obsession upvc door repair meanwood

  14. Rickysem表示:

    mexican drugstore online mexican pharmacy medication from mexico pharmacy

  15. Gaye表示:

    Leeds Window Repair Explained In Less Than 140 Characters upvc door repair
    roundhay – Gaye,

  16. 10 Places To Find Which Online Stores Ship Internationally Syncpen For Tablets

  17. 5 Killer Quora Answers On Free Casino Slots free Casino Slots

  18. Is Technology Making Mini Key Fob Replacement Better Or Worse?

    Replace mini key fob

  19. Seven Explanations On Why 12kg Washing Machines Uk Is Important
    12kg washing Machines uk

  20. What Is Lock Smith Car And Why Is Everyone Talking About It?
    mobile Locksmith car keys

  21. Ten Things You’ve Learned In Kindergarden To Help You
    Get Started With Online Work And Earn Money Side Jobs From Home Online

  22. Nine Things That Your Parent Taught You About Upvc Window Repairs Near Me upvc window Repairs Near me

  23. Michaelcew表示:

    http://canadaph24.pro/# certified canadian pharmacy

  24. Margene表示:

    Word Rewriter Tools To Help You Manage Your Everyday Life
    article rewriter ai [Margene]

  25. Dulcie表示:

    You’ll Never Guess This Renault Master Key Programming’s Benefits
    renault master key programming (Dulcie)

  26. Marcelabelp表示:

    http://canadaph24.pro/# canadian drug

  27. Rickysem表示:

    buy canadian drugs Licensed Canadian Pharmacy online canadian pharmacy

  28. Stevenrip表示:

    cheapest online pharmacy india: pharmacy website india – indian pharmacies safe

發佈留言

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