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

865,502 Responses

  1. Hey There. I found your blog using msn. This is
    an extremely well written article. I’ll be sure to bookmark
    it and return to read more of your useful info. Thanks for the post.
    I’ll definitely comeback.

  2. Wonderful goods from you, man. I’ve understand your stuff previous to and
    you’re just extremely magnificent. I actually like what
    you have acquired here, really like what you are saying and the
    way in which you say it. You make it entertaining and you still take care of to keep it smart.
    I can not wait to read far more from you. This is actually a
    terrific site.

  3. Hi to all, it’s really a good for me to pay a quick visit this
    website, it contains priceless Information.

  4. My brother suggested I might like this blog. He was
    once entirely right. This post actually made my day.
    You can not believe just how a lot time I had spent for this
    information! Thank you!

  5. Great post, I think website owners should larn a lot from this
    web blog its really user friendly. So much great info on here :
    D.

  6. I’m not that much of a online reader to be honest but your blogs really nice,
    keep it up! I’ll go ahead and bookmark your site to come back in the future.
    Many thanks

  7. Good site! I truly love how it is simple on my eyes and the data are well written. I
    am wondering how I could be notified whenever a new post has
    been made. I have subscribed to your feed which must do the trick!
    Have a nice day!

  8. Real informative and good complex body part of written content, now that’s user genial (:
    .

  9. controlc.com表示:

    This website definitely has all of the information and facts I wanted about this subject and didn’t know who to ask.

  10. discuss表示:

    It’s going to be ending of mine day, however before ending I am reading this impressive post to increase my experience.

  11. Good day I am so excited I found your webpage, I really found you by
    error, while I was browsing on Aol for something else, Anyways I
    am here now and would just like to say kudos for a remarkable post and
    a all round enjoyable blog (I also love the theme/design), I don’t have time to
    read it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be
    back to read a great deal more, Please do keep up the fantastic job.

  12. Electronics表示:

    Hello there! This is kind of off topic but I need some guidance from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.

    I’m thinking about making my own but I’m not sure where to start.

    Do you have any tips or suggestions? Cheers

  13. Very nice design and style and wonderful written content, hardly
    anything else we need :D.

  14. discuss表示:

    Fantastic goods from you, man. I have take note your stuff prior to and you’re
    just extremely excellent. I really like what you’ve acquired here, certainly
    like what you are saying and the way in which through which you assert it.
    You’re making it enjoyable and you continue to
    take care of to stay it wise. I can’t wait to learn much more from you.
    This is really a wonderful website.

  15. For hottest information you have to pay a quick visit world wide web and
    on web I found this web page as a best website for most up-to-date
    updates.

  16. Hi everyone, it’s my first pay a visit at this website, and paragraph is truly
    fruitful designed for me, keep up posting these articles or reviews.

  17. Having read this I believed it was very enlightening. I appreciate you spending some time and effort to put this information together.
    I once again find myself spending a lot of time both reading and commenting.

    But so what, it was still worthwhile!

  18. I’m curious to find out what blog system you’re using?
    I’m experiencing some minor security problems with my latest site and I’d
    like to find something more risk-free. Do you have any suggestions?

  19. After looking into a few of the articles on your blog, I seriously appreciate your technique of blogging.
    I saved as a favorite it to my bookmark site list and will
    be checking back soon. Please check out my website as well
    and tell me how you feel.

  20. What you composed made a ton of sense. However, what about this?
    what if you wrote a catchier title? I ain’t saying
    your information is not solid., but what if you added a
    headline to possibly grab a person’s attention? I mean JS 依照元素出現數量排序
    – 馬老師 雲端研究室 is kinda plain. You might glance
    at Yahoo’s home page and note how they write article headlines to get viewers interested.
    You might add a video or a pic or two to grab readers interested about everything’ve written. Just my
    opinion, it would make your posts a little bit more interesting.

  21. Great delivery. Great arguments. Keep up the good work.

  22. It’s wonderful that you are getting ideas from this piece of writing as well as from our dialogue made here.

  23. discuss表示:

    Great web site you’ve got here.. It’s hard to find quality
    writing like yours these days. I seriously appreciate individuals like you!
    Take care!!

  24. My spouse and I stumbled over here coming from 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 going over your web page repeatedly.

  25. I have been surfing on-line more than 3 hours lately,
    yet I never found any interesting article like yours.
    It is pretty price enough for me. In my view, if all webmasters and bloggers made just right content material as you probably
    did, the internet can be much more helpful than ever before.

  26. It’s difficult to find knowledgeable people about this
    subject, but you sound like you know what you’re talking about!

    Thanks

  27. I like the valuable information you provide in your articles.
    I’ll bookmark your weblog and check again here regularly.
    I’m quite sure I’ll learn a lot of new stuff right here!

    Best of luck for the next!

  28. I am genuinely grateful to the owner of this site who has shared this impressive piece of writing at at this time.

  29. Hey there, You’ve done a great job. I’ll definitely digg it and personally suggest to my friends.
    I’m confident they will be benefited from this website.

發佈留言

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