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

871,799 Responses

  1. Craigbup表示:

    #file_links[“C:\xrukeys\kw-darknet.txt,1,S – #file_links[“C:\xrukeys\kw-darknet.txt,1,S –

  2. Kenton表示:

    The Internet has created a wealth of new opportunities for those who enjoy playing poker.
    Online poker has seen a rapid increase in popularity in the last
    few years.

    Also visit my blog: casino (Kenton)

  3. Craigbup表示:

    #file_links[“C:\xrukeys\kw-darknet.txt,1,S – #file_links[“C:\xrukeys\kw-darknet.txt,1,S –

  4. Craigbup表示:

    #file_links[“C:\xrukeys\kw-darknet.txt,1,S – #file_links[“C:\xrukeys\kw-darknet.txt,1,S –

  5. 비트겟表示:

    whoah this weblog is fantastic i like reading your posts.
    Stay up the great work! You understand, lots of individuals are looking around for this information, you can help them greatly.

  6. experience表示:

    Online gambling is governed numerous rules and regulations.
    In the US, for example it is not possible to establish an online gambling site in the country where you live however, you can base it elsewhere.

    Also visit my homepage – experience

  7. Craigbup表示:

    #file_links[“C:\xrukeys\kw-darknet.txt,1,S – #file_links[“C:\xrukeys\kw-darknet.txt,1,S –

  8. Heya i’m for the first time here. I found this
    board and I find It really useful & it helped me out much.
    I hope to give something back and aid others like
    you aided me.

  9. Fabulous, what a web site it is! This web site provides valuable facts to us, keep it up.

    Here is my blog: Mesothelioma law

  10. Keep up the fantastic work, I read few content
    on this internet site and I conceive that your website is really interesting and contains lots of excellent info.

  11. HansNer表示:

    deep web drug markets tor marketplace

  12. HansNer表示:

    deep web drug url tor darknet

發佈留言

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