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,405,912 Responses

  1. Gilda表示:

    %%

    My page – cbd oil uk shop (Gilda)

  2. That is a great tip particularly to those fresh to the blogosphere.
    Brief but very accurate information… Many thanks for sharing this one.
    A must read post!

  3. Colette表示:

    windows repairs (Colette)

  4. 10 Wrong Answers For Common railroad cancer lawsuit settlements Lawsuit Chronic Obstructive Pulmonary Disease
    Questions Do You Know The Right Ones?

  5. repairer表示:

    %%

    my blog post :: repairer

  6. Hey! I know this is kind of off topic but I
    was wondering which blog platform are you using for this
    website? I’m getting tired of WordPress because I’ve had problems with hackers
    and I’m looking at alternatives for another platform.
    I would be great if you could point me in the direction of a
    good platform.

  7. Callum表示:

    %%

    Also visit my web site … organic cbd oil vape (Callum)

  8. %%

    Look into my page – cbd bath bombs in my area (shinhwapack.co.kr)

  9. Heya i’m for the first time here. I found this board and I find It truly helpful & it helped me out a lot.
    I hope to present something again and aid others such as you
    helped me.

  10. %%

    Feel free to surf to my web-site – Asbestos Litigation [Luxuriousrentz.Com]

  11. %%

    Take a look at my web site home from Work Jobs

  12. Thank you, I’ve just been searching for info approximately this topic for
    a long time and yours is the best I have discovered so far.

    However, what about the bottom line? Are you positive about the source?

  13. media & tech表示:

    media & tech http://accentmedia.eu

    Good day! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m definitely enjoying your blog and look forward to new posts.

  14. Hi, I think your blog might be having browser compatibility issues.
    Whenn I take a look at your sikte in Safari, it looks fine however, if opening in I.E.,
    it has some overlapping issues. I simply wanted too give you a quick heads up!
    Aside from that, wonderful blog!

    my blog post: xxxx pornos gratis

  15. click4r.com表示:

    amersham double glazing (click4r.com)

  16. %%

    Feel free to visit my web site – toyota aygo key replacement (http://vk1bi647en0fvwfy1byxw9pm.com/)

  17. asspuppies表示:

    I rеally like whatt yоu guys tend to be upp too. This kind
    of clever work and exposure! Keep up the ɑmazing works
    guys I’ve includeԁ you guhys to my own Ƅlogroll.

發佈留言

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