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
A motivating discussion is definitely worth comment.
I think that you should write more on this subject, it might
not be a taboo matter but usually people do not discuss such subjects.
To the next! Cheers!!
Great post.
I have been surfing online greater than three hours today, yet I
never discovered any fascinating article like yours.
It is beautiful worth sufficient for me. In my view, if all webmasters and bloggers made good content as you probably
did, the internet might be much more useful than ever before.
Hi it’s me, I am also visiting this web page regularly, this
web site is genuinely good and the users are in fact sharing good
thoughts.
This is a topic that is near to my heart… Cheers! Exactly where
are your contact details though?
This is a topic which is close to my heart… Many thanks!
Where are your contact details though?
Excellent blog! Do you have any tips and hints for aspiring writers?
I’m planning to start my own site soon but I’m
a little lost on everything. Would you advise starting with a free platform like WordPress or go for a
paid option? There are so many choices out there that I’m completely confused ..
Any ideas? Thanks!
This piece of writing gives clear idea in favor of the
new visitors of blogging, that truly how to do blogging.
Hi there, all is going sound here and ofcourse every one is sharing information, that’s really fine, keep up writing.
hi!,I love your writing so so much! proportion we keep up a correspondence extra approximately your article on AOL?
I require a specialist on this area to resolve my problem.
May be that’s you! Having a look forward to see you.
Hello mates, its fantastic article about teachingand fully defined,
keep it up all the time.
Whoa! This blog looks exactly like my old one! It’s on a completely different topic but it has pretty much the same
layout and design. Outstanding choice of colors!
This is a topic which is close to my heart… Cheers! Exactly where are your contact details though?
wonderful publish, very informative. I wonder why the opposite specialists of this sector do
not realize this. You should proceed your writing.
I am sure, you’ve a great readers’ base already!
Its like you read my mind! You appear to know so much about this, like you wrote the book in it
or something. I think that you could do with a few pics to drive the message home
a little bit, but instead of that, this is magnificent blog.
A fantastic read. I will definitely be back.
Thanks for sharing your thoughts. I really
appreciate your efforts and I am waiting for your next post thanks once again.
We’re a gaggle of volunteers and opening a brand new scheme in our
community. Your website offered us with useful info to work on. You’ve performed an impressive activity and our whole group shall
be grateful to you.
Hey there, I think your website 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!
Your way of describing everything in this post is in fact nice, every one can effortlessly know it, Thanks a lot.
Way cool! Some very valid points! I appreciate you writing this post plus the rest of the site is really good.
These are genuinely wonderful ideas in about blogging.
You have touched some fastidious things here. Any way keep up
wrinting.
Awesome! Its genuinely amazing piece of writing, I have got much clear idea
concerning from this post.
Hi, Neat post. There is a problem together with your
web site in internet explorer, might check this?
IE nonetheless is the marketplace chief and a huge component
to folks will pass over your fantastic writing due to this problem.
This is a topic that’s near to my heart… Best
wishes! Exactly where are your contact details though?
We’re a group of volunteers and starting a new scheme in our community.
Your web site offered us with helpful info to work
on. You’ve performed an impressive task and our entire neighborhood will probably be thankful to you.
Please let me know if you’re looking for a writer for your blog.
You have some really good posts and I believe
I would be a good asset. If you ever want
to take some of the load off, I’d really like to write some articles for your blog in exchange for a
link back to mine. Please shoot me an email if interested.
Many thanks!
Do you mind if I quote a couple of your articles
as long as I provide credit and sources back to your website?
My website is in the exact same niche as yours and my visitors would genuinely
benefit from some of the information you provide here.
Please let me know if this alright with you. Cheers!
you’re truly a excellent webmaster. The website loading velocity is amazing.
It sort of feels that you’re doing any distinctive trick.
Also, The contents are masterwork. you’ve done a excellent task on this topic!
I’m gone to tell my little brother, that he should also
pay a quick visit this website on regular basis to obtain updated from most
up-to-date information.
This post will help the internet visitors for setting up new weblog
or even a weblog from start to end.