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
Thanks for the auspicious writeup. It in truth was
a enjoyment account it. Glance complicated to far introduced
agreeable from you! By the way, how can we keep up a correspondence?
Appreciating the commitment you put into your website and detailed information you present.
It’s great to come across a blog every once in a while that isn’t the same old
rehashed information. Wonderful read! I’ve saved your site and I’m
adding your RSS feeds to my Google account.
I’m gone to say to my little brother, that he should also pay a
quick visit this weblog on regular basis to get updated
from most up-to-date reports.
Thanks for the auspicious writeup. It in truth was
a amusement account it. Glance complicated to far brought agreeable from
you! However, how could we be in contact?
Do you have a spam problem on this blog; I also am a blogger, and I was wanting to know your situation; we have created some nice methods and we are looking to
trade methods with other folks, be sure to shoot
me an e-mail if interested.
Thanks for the auspicious writeup. It actually used to
be a entertainment account it. Look complicated
to more added agreeable from you! However, how could we keep up a correspondence?
You’re so cool! I don’t suppose I have read through anything like this before.
So wonderful to discover another person with a few unique thoughts on this topic.
Seriously.. thank you for starting this up. This website is one thing that is needed on the internet,
someone with a bit of originality!
Hello, i believe that i saw you visited my weblog thus i came to return the want?.I’m trying to
in finding things to improve my site!I assume its
good enough to use a few of your ideas!!
Hi there colleagues, its impressive paragraph on the topic of tutoringand completely defined,
keep it up all the time.
Hello There. I discovered your blog the use of msn. This is a very well written article.
I will make sure to bookmark it and return to learn extra of your
helpful info. Thank you for the post. I will definitely comeback.
Every weekend i used to pay a visit this web page, as i wish for enjoyment, for the reason that this this web page conations really nice funny material too.
Valuable info. Fortunate me I discovered your site by accident, and
I’m shocked why this accident didn’t happened in advance!
I bookmarked it.
Thanks a bunch for sharing this with all folks you actually realize what
you’re talking approximately! Bookmarked. Kindly also talk over with my
web site =). We will have a link alternate agreement between us
Hello mates, good piece of writing and pleasant arguments
commented here, I am genuinely enjoying by these.
Hello, just wanted to mention, I liked this post.
It was practical. Keep on posting!
What’s up, after reading this remarkable paragraph i am also happy to share my experience here
with mates.
I always used to study piece of writing in news papers but
now as I am a user of internet thus from now I am using net for articles, thanks to web.
WOW just what I was looking for. Came heree by searching for https://www.quora.com/profile/Deawlink
Nice post. I was checking continuously this blog and I’m impressed!
Extremely useful info particularly the ultimate part 🙂
I handle such info a lot. I was seeking
this particular info for a long time. Thank you and good luck.
Thanks very nice blog!
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 magnificent blog. An excellent read.
I’ll certainly be back.
Fantastic blog you have here but I was wanting to know if you knew
of any message boards that cover the same topics talked about
in this article? I’d really love to be a part of
community where I can get responses from other knowledgeable people that share the same
interest. If you have any recommendations, please let me know.
Thanks a lot!
May I simply say what a relief to find someone that truly knows what they are talking about over the internet.
You definitely know how to bring an issue to light and
make it important. More people really need to look at this
and understand this side of the story. It’s surprising you are not more popular given that you certainly have the gift.
appealing review. Furthermore visit my site to possess
game slot. These pages has received around the world recognition right
from slot deposit pulsa gambling participants in Dalam negeri.
Truth be told there are several wonderful promos as you join these days.
Hey very interesting blog!
Hello to all, for the reason that I am genuinely keen of reading this website’s
post to be updated on a regular basis. It carries fastidious information.
It?s difficult to find knowledgeable people about this
topic, however, you seem like you know what you?re
talking about! Thanks
I’ve been browsing online more than 3 hours nowadays,
yet I by no means discovered any fascinating article like yours.
It’s beautiful worth enough for me. In my view, if all site owners and bloggers made excellent content material as
you probably did, the net shall be a lot more useful than ever before.
I used to be able to find good information from your
articles.