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
Sweet blog! I found it while browsing on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Thank you
An intriguing discussion is worth comment. I do believe that
you ought to write more on this subject matter, it might not be a taboo subject but usually
people don’t discuss these issues. To the next! All the best!!
This excellent website certainly has all the info I needed about this subject and didn’t know who to ask.
Hello There. I found your blog using msn. This is a really
well written article. I will make sure to bookmark it and come
back to read more of your useful info. Thanks for the post.
I will definitely return.
Your style is really unique in comparison to other
folks I’ve read stuff from. Thanks for posting when you have the opportunity, Guess I
will just bookmark this blog.
Great blog here! Also your web site loads up fast!
What host are you using? Can I get your affiliate link to your host?
I wish my site loaded up as fast as yours lol
Hey there would you mind stating which blog platform you’re working with?
I’m looking to start my own blog soon but I’m having a difficult
time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs
and I’m looking for something completely unique.
P.S Apologies for getting off-topic but I had
to ask!
I used to be able to find good information from
your blog posts.
Stunning quest there. What happened after? Good luck!
What a material of un-ambiguity and preserveness of precious know-how about unpredicted feelings.
Der Skibus hält direkt vorm Haus. Hier findet
Ihr die passenden Pensionen, Ferienhäuser
& Skihotels. Die Preise gelten pro Person und pro Woche. Mitten mittig von Stumm im Zillertal befindet sich unser
Appartementhaus Kaltenbach-Stumm mit gut ausgestatteten Ferienwohnungen. Wir Unterricht geben unseren aktuellen Angeboten weitere tolle Skireisen für Euren Skiurlaub im Zillertal.
Nicht das Richtige für Euch? Also lasst die Seele baumeln, denn
die Sauna mit Ruheraum kann kostenfrei genutzt werden. Die Preise gelten pro
Kopf und pro Woche. Die komfortablen Appartements für 4-9 Personen sind großzügig und
gemütlich eingerichtet. Der Skibus hält ca. Gerne!
Ob Gruppenreise, Ski-Kurzurlaub oder einfach nur günstige Skireisen: es gibt die passenden freien Unterkünfte im
Zillertal und den restlichen Alpen für Euren Winterurlaub im Programm.
Die Aufenthalte sind inkl. 2,5 km vom Zentrum von Mayrhofen entfernt.
Skipass buchbar – auch die Kurztripps! Mit unseren Appartements wird Eure Skireise
zum reinsten Wellness-Urlaub. Per pedes sind Gasthöfe,
Restaurants, Geschäfte und der Supermarkt bequem zu erreichen.
Wollt Ihr noch mehr? 50 m vom Haus entfernt.
Excellent, what a blog it is! This blog provides useful information to us, keep it up.
I feel that is among the so much vital information for me.
And i am glad reading your article. However want to
statement on some normal issues, The site taste is wonderful, the articles is really excellent : D.
Good activity, cheers
I was pretty pleased to find this web site. I want to
to thank you for your time for this fantastic read!! I definitely loved every bit of it and i also have you
book-marked to look at new things on your website.
I used to be able to find good info from your content.
Great post.
Hey very nice blog!
We’re a group of volunteers and starting a new scheme in our community.
Your site provided us with valuable info to work on. You
have performed a formidable activity and our whole community might be grateful to you.
Hi fantastic website! Does running a blog such as this take a massive amount work?
I’ve very little expertise in computer programming however I was hoping to
start my own blog in the near future. Anyhow, if you have any
recommendations or techniques for new blog owners please share.
I understand this is off topic but I simply had to ask.
Kudos!
I’ve been browsing online more than three hours today, yet
I never found any interesting article like yours.
It’s pretty worth enough for me. Personally, if all
website owners and bloggers made good content as you did, the internet
will be much more useful than ever before.
I must thank you for the efforts you have put in writing this site.
I really hope to see the same high-grade blog posts by you in the future as well.
In truth, your creative writing abilities has motivated me to get my own, personal website
now 😉
I’m not that much of a online reader to be honest but your sites really nice, keep it
up! I’ll go ahead and bookmark your site to come back later on. Cheers
Hello there! This is kind of off topic but I need some help from an established blog.
Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
I’m thinking about setting up my own but I’m not sure where to begin. Do you have any ideas or suggestions?
Appreciate it
Hi it’s me, I am also visiting this web page on a
regular basis, this site is actually good and the visitors are actually sharing nice thoughts.
I don’t even know the way I stopped up right here, however I believed this submit was once great.
I don’t recognise who you are however certainly you’re going
to a famous blogger if you aren’t already.
Cheers!
Hi, i think that i saw you visited my weblog thus i came to “return the favor”.I’m trying to find
things to enhance my website!I suppose its ok to use a few of your ideas!!
Normally I do not learn post on blogs, but I would like to
say that this write-up very pressured me to check out and do so!
Your writing style has been amazed me. Thank you,
very nice article.
Pretty section of content. I just stumbled upon your blog and in accession capital to assert that I get
in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I
achievement you access consistently rapidly.
I think the admin of this web page is in fact working hard for his web site,
as here every stuff is quality based data.
Awesome issues here. I’m very satisfied to peer your post.
Thank you a lot and I am having a look forward to contact you.
Will you please drop me a mail?