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,368,152 Responses

  1. Reuters Today表示:

    My brother recommended I may like this website. He used to
    be totally right. This post actually made my day. You can not imagine
    simply how much time I had spent for this
    information! Thanks!

  2. %%

    My page; Car key cover volkswagen (http://www.koreafurniture.com)

  3. You actually stated that adequately.

    Here is my webpage https://boy-fuck.com

  4. Thanks extremely useful. Will certainly share website with my friends.

    https://www.polskibiznes.info

  5. Francesca表示:

    %%

    My web-site: fresh espresso beans; Francesca,

  6. Ramiro Seder表示:

    merci beaucoup !

  7. What’s up to all, the contents existing at this site are truly awesome for people
    knowledge, well, keep up the nice work fellows.

  8. Great goods from you, man. I have have in mind your stuff previous to and
    you are simply too excellent. I really like what you’ve bought right here,
    certainly like what you’re stating and the way in which you
    assert it. You make it enjoyable and you still take care of to stay it wise.

    I can not wait to learn much more from you. This is really a terrific site.

  9. I feel this is among the most important information for
    me. And i am glad reading your article. But want to remark on few general
    things, The site style is great, the articles is actually great :
    D. Excellent task, cheers

  10. Leewhan.Com表示:

    %%

    My site Skoda Key Replacement – Leewhan.Com,

  11. Xavier表示:

    %%

    Have a look at my web site; cheapest full spectrum
    cbd oil uk (Xavier)

  12. Gold Ira Best表示:

    Everyone loves what you guys tend to be up too. This sort of clever work and coverage!

    Keep up the very good works guys I’ve included you guys to blogroll.

  13. Good way of explaining, and pleasant post to get facts concerning my presentation topic, which i am going to convey in academy.

  14. You should take part in a contest for one of the finest blogs on the web.

    I most certainly will highly recommend this site!

  15. Te.Legra.Ph表示:

    Upvc Window Repairs Macclesfield (Te.Legra.Ph)

  16. Erwin表示:

    %%

    my web page; cs case opening (Erwin)

發佈留言

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