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
you are actually a just right webmaster. The site loading speed
is incredible. It seems that you’re doing any distinctive trick.
In addition, The contents are masterpiece.
you’ve performed a excellent process in this matter!
Hey there this is kind of of off topic but I was wanting
to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding knowledge so I wanted
to get advice from someone with experience. Any help would be enormously appreciated!
Good day! I simply want to offer you a huge thumbs up for the great
information you have got right here on this post. I will be returning to your blog for more soon.
I pay a quick visit every day some websites and information sites to read
content, however this weblog provides feature based articles.
Hello my family member! I wish to say that this post is awesome, nice written and include
approximately all vital infos. I’d like to see more posts like this .
Hello! 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 bookmarking and will be tweeting this to my followers!
Wonderful blog and outstanding design and style.
Simply desire to say your article is as surprising.
The clearness in your put up is simply nice and i
can suppose you are an expert on this subject.
Well together with your permission allow me to clutch
your RSS feed to keep up to date with coming near near post.
Thank you 1,000,000 and please keep up the rewarding work.
Hi there! I know this is kinda off topic nevertheless I’d figured I’d
ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa?
My site addresses a lot of the same topics as yours and
I feel we could greatly benefit from each other. If
you happen to be interested feel free to send me an email.
I look forward to hearing from you! Excellent blog by the way!
Incredible quest there. What happened after?
Take care!
Asking questions are truly good thing if you
are not understanding something completely, however this paragraph offers pleasant understanding yet.
I really like your blog.. very nice colors & theme.
Did you design this website yourself or did you hire someone
to do it for you? Plz reply as I’m looking to create my own blog and would like to know where u got this from.
many thanks
Genuinely when someone doesn’t know after
that its up to other users that they will help, so here it
takes place.
My brother suggested I might like this website. He was totally right.
This publish truly made my day. You cann’t consider just how much time I had spent
for this information! Thank you!
I do not even know the way I stopped up right here, but I assumed this put up was once good.
I don’t realize who you might be but certainly you are going to a famous blogger if you
happen to aren’t already. Cheers!
bookmarked!!, I love your blog!
My brother recommended I might like this blog. He was once totally right.
This post truly made my day. You cann’t believe simply how
a lot time I had spent for this information! Thank you!
Howdy just wanted to give you a quick heads up
and let you know a few of the images 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 outcome.
Greate post. Keep posting such kind of information on your page.
Im really impressed by your site.
Hello there, You’ve done a fantastic job. I will definitely
digg it and individually recommend to my friends. I’m confident they will be benefited from this web site.
Hey this is kind of of off topic but I was wanting to know if blogs use
WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I wanted to get advice from someone with experience.
Any help would be enormously appreciated!
It’s truly a great and helpful piece of info. I am glad that you just shared this helpful info with us.
Please stay us informed like this. Thanks for sharing.
Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your web site is fantastic,
as well as the content!
It’s very straightforward to find out any matter on net as compared to books, as I found this article at this site.
Good day! I could have sworn I’ve visited this web site before but after going through many of the articles I realized it’s new
to me. Anyhow, I’m certainly delighted I came across it and I’ll be bookmarking it and checking
back frequently!
What’s up, after reading this amazing post i am too happy to share my
know-how here with friends.
That is a great tip particularly to those fresh to the blogosphere.
Simple but very precise info… Appreciate your sharing this one.
A must read article!
Hey I know this is off topic but I was wondering if you knew of
any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and was hoping maybe
you would have some experience with something like this. Please let me know if
you run into anything. I truly enjoy reading your blog and I look forward to your new updates.
Hi there! This post couldn’t be written any better! Reading through this post reminds me of my old room mate!
He always kept chatting about this. I will forward this page to him.
Fairly certain he will have a good read. Many thanks for sharing!
Today, while I was at work, my sister stole my iphone and tested to see if it can survive a 25 foot
drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.
I know this is totally off topic but I had to share it with
someone!
I have been surfing online more than 3 hours today,
yet I never found any interesting article like yours.
It is pretty worth enough for me. In my view, if all website owners and bloggers
made good content as you did, the net will be a lot more useful
than ever before.
Hi there to all, how is all, I think every one
is getting more from this web page, and your views are nice in favor of new
visitors.