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
Howdy would you mind stating which blog platform you’re working with?
I’m going to start my own blog soon but I’m having a tough time making a decision 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
unique. P.S Sorry for getting off-topic but
I had to ask!
It’s nearly impossible to find well-informed people in this particular subject, however, you seem like you know what you’re talking about!
Thanks
Hi there! Someone in my Facebook group shared this website with
us so I came to look it over. I’m definitely
enjoying the information. I’m book-marking and will be tweeting this to my followers!
Excellent blog and fantastic design.
Spot on with this write-up, I seriously feel this site needs much more attention. I’ll probably be returning to read through more, thanks for the
advice!
I don’t know whether it’s just me or if perhaps everybody else
encountering issues with your site. It appears as if some
of the text on your content are running off the screen. Can somebody else please comment and let
me know if this is happening to them too? This may be a problem with my web browser because I’ve had this happen previously.
Cheers
Hi there! Would you mind if I share your blog with my myspace group?
There’s a lot of people that I think would really enjoy your content.
Please let me know. Thanks
of course like your web-site however you need to take a look
at the spelling on quite a few of your posts. Several of them are rife with
spelling problems and I in finding it very troublesome to inform the truth on the other hand I will definitely come again again.
Hello! This is my first visit to your blog!
We are a group of volunteers and starting a new project in a community in the
same niche. Your blog provided us valuable information to work on.
You have done a extraordinary job!
Appreciate the recommendation. Will try it out.
We are a gaggle of volunteers and opening a new scheme in our community.
Your site offered us with helpful information to work
on. You’ve performed an impressive job and our entire neighborhood will likely be grateful to you.
Fantastic beat ! I wish to apprentice at the same time as you amend your website,
how can i subscribe for a weblog site? The account helped me a acceptable deal.
I have been a little bit acquainted of this your broadcast
provided bright clear concept
Having read this I thought it was rather enlightening.
I appreciate you spending some time and effort to put this informative article together.
I once again find myself personally spending a lot of time both reading
and commenting. But so what, it was still worthwhile!
Wow, wonderful blog format! How lengthy have you been running a blog for?
you made running a blog look easy. The entire look of your website is wonderful, as neatly as the content material!
We are a group of volunteers and opening a new scheme in our community.
Your site provided us with valuable information to work on. You have done a formidable job and our
whole community will be thankful to you.
Yes! Finally something about situs judi slot online.
Hey very cool site!! Man .. Beautiful .. Superb ..
I’ll bookmark your website and take the feeds also?
I’m satisfied to search out so many helpful info right here within the submit,
we’d like work out more strategies on this regard, thank
you for sharing. . . . . .
Hello to all, it’s really a nice for me to visit this
website, it contains important Information.
Pretty nice post. I simply stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts.
After all I will be subscribing for your rss feed and I am
hoping you write once more very soon!
Highly energetic article, I liked that bit. Will there be a part 2?
Thank you, I’ve recently been looking for information about this
subject for a long time and yours is the greatest I have
found out till now. However, what about the bottom line?
Are you positive concerning the source?
It’s awesome to pay a quick visit this web page and reading the views of all
friends concerning this piece of writing, while I am also
zealous of getting knowledge.
Wonderful goods from you, man. I have consider your stuff previous to and you are just extremely great.
I actually like what you’ve received right here, certainly like
what you are saying and the way in which in which you say
it. You’re making it entertaining and you continue to take care of to stay it wise.
I can’t wait to read far more from you. That is really a terrific website.
Good way of explaining, and pleasant paragraph
to get facts concerning my presentation topic, which i
am going to present in institution of higher education.
Sweet blog! I found it while surfing around on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem
to get there! Many thanks
It’s awesome in support of me to have a website, which is helpful designed for my
knowledge. thanks admin
What’s up Dear, are you genuinely visiting this
website regularly, if so then you will without doubt take good knowledge.
Definitely consider that that you stated. Your favorite reason appeared to be on the web the simplest factor
to be aware of. I say to you, I certainly get irked whilst people think about concerns that they plainly
do not understand about. You managed to hit the nail upon the top and
also defined out the entire thing without having
side-effects , other people can take a signal. Will likely be again to get more.
Thanks
Hi there just wanted to give you a quick heads up and let you
know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different browsers and both show the same results.
I think the admin of this site is in fact working hard
in favor of his web site, since here every stuff is quality based information.
If you are going for most excellent contents like I do, just visit
this website all the time since it provides quality contents, thanks