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,373,743 Responses

  1. Pedroereva表示:

    reputable mexican pharmacies online: mexican pharmaceuticals online – mexico pharmacy

  2. Michaelcew表示:

    https://canadaph24.pro/# canadian drugs

  3. Pedroereva表示:

    buying from online mexican pharmacy: mexico pharmacy – mexican pharmacy

  4. Guide To Auto Accident Attorney: The Intermediate Guide For Auto Accident Attorney Auto Accident Attorney

  5. Marcelabelp表示:

    http://canadaph24.pro/# canadian pharmacy meds reviews

  6. Pedroereva表示:

    reputable mexican pharmacies online: mexico drug stores pharmacies – best online pharmacies in mexico

  7. What Freud Can Teach Us About Shop Anal Toy Anal toys shop (http://www.designdarum.co.kr)

  8. Nine Things That Your Parent Taught You About
    Birth Injury Lawsuit birth Injury lawsuit

  9. Guide To Volkswagen Key Cutting Service Near Me: The Intermediate Guide To Volkswagen Key Cutting Service Near Me Volkswagen key cutting Service near me

  10. What Is Semi Truck Claim And How To Use What Is Semi Truck Claim And How To Use
    Semi Truck Accident Lawyers

  11. The Most Inspirational Sources Of Malpractice Settlement malpractice Lawyers

  12. Jere表示:

    The Most Common Window Repair Near Mistake Every Beginner
    Makes upvc window repair (Jere)

  13. Designer Handbags Beige Tools To Help You Manage
    Your Daily Life Designer Handbags Beige Trick That Everyone Should Learn designer handbags beige

  14. Marcelabelp表示:

    https://indiaph24.store/# cheapest online pharmacy india

  15. Marcelabelp表示:

    http://mexicoph24.life/# buying prescription drugs in mexico online

  16. Here’s A Little Known Fact About Online Clothes Shopping Websites Uk.

    Online Clothes Shopping Websites Uk car lift bottle jack

  17. JustinCit表示:

    best online pharmacy india http://indiaph24.store/# buy prescription drugs from india
    top 10 online pharmacy in india

  18. Be On The Lookout For: How Work From Home Jobs Uk Is Taking Over And What We Can Do About It weekend work From home

  19. JustinCit表示:

    india pharmacy https://indiaph24.store/# best india pharmacy
    Online medicine home delivery

  20. web page表示:

    I believe what you posted was very logical. But,
    think on this, suppose you composed a catchier title?
    I mean, I don’t want to tell you how to run your website, but suppose you added
    a title that makes people want more? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is kinda
    boring. You could peek at Yahoo’s front page and note how they write news headlines to get viewers interested.
    You might try adding a video or a related pic or two to get people interested about what
    you’ve written. Just my opinion, it would make your website
    a little livelier.

  21. Marcelabelp表示:

    http://indiaph24.store/# Online medicine home delivery

  22. How Double Glazing Installers Near Me Transformed My Life For The Better repair double glazing Window

  23. Marcelabelp表示:

    http://mexicoph24.life/# mexican online pharmacies prescription drugs

  24. The 10 Scariest Things About Double Glazing Replacement Window double glazing replacement Window

  25. JustinCit表示:

    indianpharmacy com https://indiaph24.store/# india pharmacy mail order
    indian pharmacy

  26. Michaelcew表示:

    http://canadaph24.pro/# safe canadian pharmacies

  27. What Is Truck Accident? History Of Truck Accident truck Accident lawsuits

  28. Olga表示:

    How Double Glazed Units Near Me Became The Hottest Trend Of 2023 replacement
    double glazed units near me – Olga

  29. Pedroereva表示:

    purple pharmacy mexico price list: mexican pharmacy – mexican drugstore online

  30. Hi, I do believe this is an excellent site. I
    stumbledupon it 😉 I will return once again since I saved as a
    favorite it. Money and freedom is the greatest way
    to change, may you be rich and continue to guide others.

發佈留言

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