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
Hi there! This blog post could not be written much better! Looking at this article reminds me of
my previous roommate! He constantly kept talking about this.
I will send this post to him. Fairly certain he’s going to have a very good read.
Many thanks for sharing!
Hi there i am kavin, its my first occasion to commenting anywhere, when i read this article i thought i could also
make comment due to this sensible article.
My family members all the time say that I am wasting my time here
at web, however I know I am getting knowledge daily by reading such nice content.
Amazing issues here. I am very satisfied to look your post.
Thank you a lot and I am taking a look forward to touch you.
Will you please drop me a e-mail?
I like the helpful information you provide in your articles.
I’ll bookmark your weblog and check again here regularly.
I’m quite sure I’ll learn many new stuff right
here! Best of luck for the next!
It is perfect time to make some plans for the future and it is time to
be happy. I’ve learn this put up and if I may just I wish to counsel you some fascinating issues or advice.
Maybe you can write subsequent articles regarding this article.
I desire to learn more things approximately it!
Hello to all, it’s genuinely a fastidious for me to visit this web site, it contains helpful Information.
Hey there, I think your website might be having browser compatibility issues.
When I look at your blog site in Safari, 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, awesome blog!
Thanks a bunch for sharing this with all of us you actually understand what you’re talking about!
Bookmarked. Kindly additionally consult with my website =).
We may have a hyperlink alternate agreement between us
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 can do with some pics to drive the message home a bit,
but other than that, this is excellent blog.
An excellent read. I will definitely be back.
Quality articles is the main to interest the visitors to visit the web page, that’s what this site is providing.
This paragraph will help the internet people for building up new website or even a weblog from start
to end.
Thank you, I’ve recently been looking for info approximately this
subject for a long time and yours is the greatest
I’ve found out till now. But, what concerning the
conclusion? Are you sure concerning the source?
My brother suggested I might like this blog. He was once totally right.
This post truly made my day. You cann’t believe simply how so much time I had spent for this information!
Thanks!
For most recent information you have to visit internet and
on web I found this site as a best site for most up-to-date updates.
Every weekend i used to pay a visit this web page, because
i want enjoyment, for the reason that this this web page conations really pleasant funny material too.
As the admin of this website is working, no uncertainty very soon it will be well-known,
due to its quality contents.
Hurrah! Finally I got a blog from where I know
how to truly get useful information regarding my study
and knowledge.
I got this web site from my pal who shared with me concerning this website and now this time I am visiting this
site and reading very informative articles or reviews here.
It’s difficult to find experienced people in this particular subject, but you sound like you know what you’re talking about!
Thanks
What’s up, for all time i used to check webpage
posts here in the early hours in the daylight,
since i love to gain knowledge of more and more.
Excellent website. Plenty of useful information here.
I am sending it to a few friends ans additionally sharing in delicious.
And naturally, thanks for your sweat!
Nice post. I was checking constantly this blog and I am impressed!
Very useful information particularly the last part 🙂 I care for such info
a lot. I was looking for this particular
information for a very long time. Thank you and best of luck.
hello!,I like your writing so so much! percentage we keep up a correspondence more about your post on AOL?
I need an expert in this house to solve my problem.
May be that’s you! Looking ahead to look you.
Hi there friends, its fantastic piece of writing regarding educationand entirely explained,
keep it up all the time.
Howdy! I could have sworn I’ve visited this website before but
after looking at some of the posts I realized it’s new to me.
Nonetheless, I’m certainly pleased I came across it and I’ll
be book-marking it and checking back frequently!
Your means of explaining everything in this article is in fact fastidious, every
one can easily be aware of it, Thanks a lot.
I love your blog.. very nice colors & theme. Did you create this
website yourself or did you hire someone to do it for
you? Plz reply as I’m looking to design my own blog and would like
to know where u got this from. kudos
Howdy, i read your blog occasionally and i own a similar one and i was
just curious if you get a lot of spam responses? If so how do you protect against it, any plugin or anything you can recommend?
I get so much lately it’s driving me mad so any support is very much appreciated.
Have you ever thought about writing an ebook
or guest authoring on other websites? I have a blog based upon on the same subjects you discuss and
would love to have you share some stories/information. I know my audience would enjoy
your work. If you’re even remotely interested,
feel free to shoot me an e mail.