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
The amount of a simple money mortgage is restricted.
buying cheap propecia get cheap propecia without dr prescription propecia price
buying cheap propecia without dr prescription: buy cheap propecia without a prescription – order generic propecia price
I simply couldn’t leave your web site prior to suggesting that
I actually enjoyed the standard information a person provide to
your visitors? Is gonna be again continuously to check up on new posts
lisinopril canada: lisinopril 20 mg uk – lisinopril generic price comparison
How To Create Successful Fridge Freezer Bosch Tips From Home fridge Freezer Collection
purchase cytotec: buy cytotec online fast delivery – buy misoprostol over the counter
https://cytotec.club/# cytotec online
https://ciprofloxacin.tech/# buy cipro cheap
9 . What Your Parents Taught You About Green Power Mobility
Scooters Uk green power Mobility scooters uk
A Look At The Secrets Of 4 Wheel Drive Mobility Scooter 4
wheeler mobility scooter [Reynaldo]
http://finasteride.store/# cost cheap propecia prices
http://nolvadex.life/# nolvadex pills
Misoprostol 200 mg buy online: buy cytotec in usa – buy cytotec over the counter
https://cytotec.club/# cytotec abortion pill
15 Fun And Wacky Hobbies That’ll Make You More Successful At Greenpower Mobility Mobility Power
http://finasteride.store/# order propecia without a prescription
https://ciprofloxacin.tech/# cipro ciprofloxacin
http://finasteride.store/# cost cheap propecia without a prescription
lisinopril pill lisinopril average cost lisinopril 30 mg tablet
Why Everyone Is Talking About Glass Repair Leeds Right Now upvc door repair roundhay, Derrick,
http://nolvadex.life/# femara vs tamoxifen
over the counter lisinopril lisinopril 80 mg tablet where to buy lisinopril 2.5 mg
http://cytotec.club/# purchase cytotec
Then You’ve Found Your Erb’s Palsy Legal … Now What?
erb’s Palsy lawyers
https://cytotec.club/# purchase cytotec
http://nolvadex.life/# tamoxifen 20 mg
http://nolvadex.life/# tamoxifen cost
https://nolvadex.life/# effexor and tamoxifen
https://finasteride.store/# order generic propecia for sale