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...

996,846 Responses

  1. ElmerSok表示:

    Kaka https://kaka.prostoprosport-br.com Futebolista brasileiro, meio-campista. O apelido “Kaka” e um diminutivo de Ricardo. Formado em Sao Paulo. De 2002 a 2016, integrou a Selecao Brasileira, pela qual disputou 92 partidas e marcou 29 gols. Campeao mundial 2002.

  2. Medicines information leaflet. Short-Term Effects.
    where to buy cheap arimidex without insurance
    All about pills. Read information now.

  3. If you wish for to take a great deal from this post then you have to apply such techniques to your won weblog.

  4. Drugs prescribing information. Cautions.
    buying generic lisinopril without rx
    Best what you want to know about pills. Get now.

  5. The Most Underrated Companies To Monitor In The Greenpower Mobility Industry Green Power Scooter

  6. Slot90表示:

    My partner and I stumbled over here by a different web
    address and thought I may as well check things out. I like what I see so
    now i am following you. Look forward to looking
    into your web page again.

  7. The 10 Scariest Things About Truck Accident Law Firm truck Accident law Firm

  8. Pills information leaflet. Short-Term Effects.
    get cheap esomeprazole without rx
    Everything about medicine. Read now.

  9. The 10 Scariest Things About Best Slots Best Slots (Merigotoya.Hatenablog.Com)

  10. This Story Behind Window Repair Near Will Haunt You For
    The Rest Of Your Life! upvc Window repair Near me

  11. kraken тор表示:

    Hello I am so excited I found your blog, I really found you by accident,
    while I was researching on Yahoo for something else, Nonetheless I
    am here now and would just like to say cheers for a fantastic post and a all round exciting blog (I also love the
    theme/design), I don’t have time to browse it all at the minute but I have bookmarked it
    and also added in your RSS feeds, so when I have time I will
    be back to read much more, Please do keep up the awesome jo.

  12. Asbestos case表示:

    Asbestos cases can be complicated because they require multiple
    jurisdictions. Law firms with experience in processing Asbestos case claims at the national and state levels know the specific
    laws that apply.

  13. Guide To Workers Compensation Attorney: The Intermediate Guide The Steps To Workers Compensation Attorney workers compensation

  14. See What Cerebral Palsy Lawyer Tricks The Celebs Are Using
    Cerebral Palsy Lawyer

  15. The 10 Most Scariest Things About Private ADHD Assessment Adult private adhd assessment adult

  16. What The 10 Most Worst High Wycombe Door Panels Errors Of All Time Could Have Been Prevented glazier High wycombe

  17. Medication information sheet. Brand names.
    buy generic synthroid pill
    All what you want to know about medicine. Get now.

  18. Medicament information for patients. Cautions.
    get cheap atarax price
    Actual trends of medicine. Read now.

  19. Pills information for patients. Effects of Drug Abuse.
    can i order generic norvasc without prescription
    All information about drug. Get information here.

  20. attorney表示:

    Guide To 18 Wheeler Accident Attorney: The
    Intermediate Guide To 18 Wheeler Accident Attorney
    attorney

  21. The One SEO Link Building Services Trick Every Person Should
    Know Seo services Pricing

  22. memek becek表示:

    Howdy! This blog post could not be written much better! Looking at this post reminds me of my previous roommate!
    He continually kept preaching about this. I’ll send this article to him.
    Fairly certain he’ll have a great read. Thank you for sharing!

  23. Roxanne表示:

    You’ll Never Guess This Window Replacement Near Me’s Tricks window replacement near
    me (Roxanne)

  24. whoah this blog is great i really like reading your articles.

    Keep up the good work! You realize, many people are looking round for this information,
    you can help them greatly.

    Feel free to visit my blog; fitspresso complaints

發佈留言

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