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
This is a topic which is near to my heart… Thank you!
Where are your contact details though?
Ahaa, its fastidious dialogue regarding this article
at this place at this blog, I have read all that, so now me also commenting at this place.
What’s up, its nice post concerning media print, we all be familiar with media is a great source
of data.
Hey, I think your website might be having browser compatibility issues.
When I look at your blog in Firefox, 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, very good blog!
This paragraph will assist the internet visitors for setting up new website or even a weblog from start to end.
Wow, wonderful blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your website is wonderful, let alone the
content!
Wow, that’s what I was seeking for, what a information! existing here at
this web site, thanks admin of this web site.
Fine way of describing, and good paragraph
to obtain information about my presentation topic,
which i am going to present in academy.
Thank you a lot for sharing this with all of us you really understand what you are talking approximately!
Bookmarked. Kindly additionally consult with my site =).
We will have a link alternate contract among us
Hi! This is my 1st comment here so I just wanted to give
a quick shout out and tell you I really enjoy reading through your posts.
Can you suggest any other blogs/websites/forums that cover the same
subjects? Thanks!
Hello there! This post could not be written any better!
Reading through this post reminds me of my previous room mate!
He always kept talking about this. I will forward this post
to him. Pretty sure he will have a good read. Thanks for sharing!
Just want to say your article is as astonishing. The clarity in your post is
just excellent and i could assume you’re an expert
on this subject. Well with your permission let me to grab your RSS feed to keep up to date
with forthcoming post. Thanks a million and please continue the rewarding work.
Asking questions are truly fastidious thing if you are
not understanding something fully, except this article presents good understanding even.
Hello Dear, are you really visiting this web site daily,
if so then you will absolutely get pleasant know-how.
Greetings from Carolina! I’m bored at work so I decided to
browse your blog on my iphone during lunch break.
I really like the information you present here and can’t wait to take a look when I get home.
I’m shocked at how quick your blog loaded on my phone ..
I’m not even using WIFI, just 3G .. Anyhow, very good
blog!
Greetings from Florida! I’m bored at work so I decided to browse your blog on my iphone during lunch break.
I love the info you present here and can’t wait to take a look when I get home.
I’m surprised at how fast your blog loaded on my phone ..
I’m not even using WIFI, just 3G .. Anyways, good blog!
It’s amazing to pay a quick visit this website
and reading the views of all mates regarding this article, while I am also zealous
of getting know-how.
It’s in fact very difficult in this full of activity life to listen news on Television, thus I simply
use world wide web for that purpose, and obtain the latest information.
When some one searches for his essential thing, therefore he/she desires
to be available that in detail, thus that thing is maintained over here.
Exceptional post however , I was wondering if you could write a
litte more on this topic? I’d be very thankful if you could elaborate a little bit further.
Bless you!
Hi there colleagues, fastidious piece of writing
and nice urging commented here, I am actually
enjoying by these.
Good web site you have got here.. It’s difficult to find high-quality writing
like yours these days. I truly appreciate people like you!
Take care!!
Your method of describing everything in this paragraph is genuinely fastidious, all be able to effortlessly be aware
of it, Thanks a lot.
With havin so much written content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content
I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the web without my agreement.
Do you know any ways to help stop content from being stolen? I’d truly appreciate it.
When someone writes an paragraph he/she keeps the idea of a user in his/her brain that how a
user can know it. Therefore that’s why this paragraph is outstdanding.
Thanks!
I’d like to thank you for the efforts you have put in penning this website.
I am hoping to check out the same high-grade blog posts by you in the
future as well. In fact, your creative writing abilities has motivated me to get my
very own site now 😉
If you are going for most excellent contents like myself, only
pay a quick visit this site all the time because
it gives feature contents, thanks
Heya i am for the primary time here. I found this board and I find It truly
useful & it helped me out much. I hope to present something
back and aid others like you aided me.
I am not sure where you are getting your information, but great topic.
I needs to spend some time learning much more or understanding more.
Thanks for excellent information I was looking for this information for my mission.
I enjoy what you guys are up too. This type of clever work and exposure!
Keep up the great works guys I’ve added you guys to my personal blogroll.