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
We are a group of volunteers and opening a new scheme in our community.
Your website offered us with valuable information to work on. You have done an impressive job and our whole community will be thankful to you.
There is certainly a lot to find out about this topic. I like all
of the points you made.
I just could not depart your website prior to suggesting that I really enjoyed the standard information a person provide on your visitors?
Is going to be back steadily to investigate cross-check new posts
Hi there, You’ve done a fantastic job. I will definitely digg it
and personally recommend to my friends. I’m sure they will be benefited
from this web site.
May I just say what a relief to uncover a person that genuinely understands what they’re talking about on the
net. You definitely realize how to bring a problem
to light and make it important. More people need to look at this and understand this side
of your story. It’s surprising you are not more popular given that you surely possess the gift.
This page definitely has all of the information I wanted concerning this subject and didn’t know who
to ask.
Howdy! This is my first comment here so I just wanted to give a quick shout out and tell you I really enjoy reading through your posts.
Can you recommend any other blogs/websites/forums that deal with the same topics?
Thank you so much!
Thank you for any other excellent article. Where else may anybody get that
kind of info in such an ideal way of writing?
I have a presentation subsequent week, and I am on the look for such
info.
Hi! I’ve been reading your web site for a while now and finally got the courage to go ahead and give
you a shout out from Houston Tx! Just wanted to tell you keep up the excellent
work!
I’m extremely impressed with your writing skills and also with
the layout on your weblog. Is this a paid theme or did
you customize it yourself? Anyway keep up the excellent
quality writing, it is rare to see a great blog like this one today.
What a data of un-ambiguity and preserveness of valuable knowledge
on the topic of unexpected feelings.
Outstanding quest there. What happened after? Good luck!
Hey! This is my first visit to your blog!
We are a team of volunteers and starting a new initiative in a community in the same niche.
Your blog provided us useful information to work on. You have done a extraordinary job!
Thank you for another informative web site. The place else could I get that type of info written in such an ideal way?
I have a challenge that I’m simply now operating on, and I have been on the glance out for such
information.
Magnificent goods from you, man. I have understand your stuff
previous to and you are just extremely wonderful. I actually like what you have acquired here, certainly like what you’re saying and the
way in which you say it. You make it entertaining and you still care for to keep it wise.
I can’t wait to read much more from you. This is actually a
tremendous site.
Highly energetic blog, I enjoyed that a lot. Will there be a part 2?
This is a very good tip particularly to those new to the
blogosphere. Short but very accurate info… Thank you for
sharing this one. A must read article!
Now I am going to do my breakfast, after having my breakfast coming yet again to read more news.
Can you tell us more about this? I’d care to find out some additional information.
I needed to thank you for this good read!! I certainly enjoyed every little
bit of it. I’ve got you bookmarked to look
at new things you post…
My partner and I absolutely love your blog and find nearly all of your post’s to be exactly what I’m looking for.
Would you offer guest writers to write content in your
case? I wouldn’t mind writing a post or elaborating on a lot of the
subjects you write concerning here. Again, awesome website!
Just desire to say your article is as astonishing. The clearness in your post is simply excellent and i can assume you are an expert
on this subject. Fine with your permission let me
to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please continue the gratifying work.
I am sure this paragraph has touched all the internet viewers,
its really really good piece of writing on building up new blog.
Having read this I believed it was really enlightening.
I appreciate you finding the time and energy to put this article
together. I once again find myself personally spending way
too much time both reading and commenting. But so what, it was still worth it!
My relatives all the time say that I am killing my time here at
net, except I know I am getting familiarity every day by reading such
nice articles.
Pretty component of content. I simply stumbled upon your
web site and in accession capital to assert
that I get actually loved account your blog posts. Anyway I
will be subscribing for your feeds or even I fulfillment you access
constantly quickly.
Hello, I think your site might be having browser compatibility issues.
When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has
some overlapping. I just wanted to give you a quick heads up!
Other then that, excellent blog!
I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. In my view, if all site owners and bloggers made
good content as you did, the web will be a lot more useful than ever before.
First of all I would like to say wonderful blog!
I had a quick question that I’d like to ask if you do not mind.
I was interested to find out how you center yourself and clear your thoughts before writing.
I have had difficulty clearing my mind in getting my thoughts out.
I do take pleasure in writing but it just seems like the first 10 to 15 minutes are generally wasted just trying to figure out how to begin. Any recommendations
or hints? Thanks!
If some one wants expert view on the topic of blogging and
site-building afterward i recommend him/her to visit this web
site, Keep up the good work.