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
Hello there, just became aware of your blog through Google,
and found that it is really informative. I’m going to watch
out for brussels. I’ll appreciate if you continue this in future.
Numerous people will be benefited from your writing.
Cheers!
I know this site presents quality dependent content and
additional data, is there any other site which presents these kinds of things in quality?
I could not refrain from commenting. Very well written!
Hello, I check your blog on a regular basis. Your story-telling style is witty, keep it up!
Thank you for the auspicious writeup. It in fact was
a amusement account it. Look advanced to more added agreeable
from you! By the way, how can we communicate?
I am curious to find out what blog platform you’re working with?
I’m experiencing some small security problems with my latest website and I’d like
to find something more safeguarded. Do you have any recommendations?
Wonderful work! That is the kind of information that are supposed to be shared across the internet.
Disgrace on the seek engines for not positioning this submit higher!
Come on over and seek advice from my website . Thank
you =)
Hurrah, that’s what I was exploring for, what a material!
present here at this blog, thanks admin of this site.
Keep on writing, great job!
Excellent article. I’m going through a few of these issues as well..
Great blog here! Also your website loads up very fast! What host are you using?
Can I get your associate hyperlink for your host?
I desire my website loaded up as quickly as yours lol
I am truly grateful to the owner of this web page who has shared this fantastic piece of writing at
at this time.
I couldn’t refrain from commenting. Perfectly written!
Greate pieces. Keep writing such kind of information on your blog.
Im really impressed by it.
Hi there, You have done a great job. I will definitely digg it
and in my opinion recommend to my friends. I’m confident they
will be benefited from this web site.
Hi there! I know this is kind of off topic but I was
wondering which blog platform are you using for this website?
I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform.
I would be fantastic if you could point me in the direction of a good platform.
Allerdings werden Nordic Walking Stöcke in Längenabständen von 5 cm (selten auch
2,5cm) angeboten, so, dass im gegebenen Fall gerundet werden muss.
Dabei tunlich gerade bei Einsteigern eher abzurunden und einen kürzeren Stock zu wählen. Als erfahrener Trainer kann
man gerade durch (sinnvolle!) Variation der Stocklänge unterschiedliche Trainingsreize setzen. Fortgeschrittene dynamische Nordic Walker können ruhig auch mit einem größeren Stock arbeiten. Für
Anfänger ist der Winkel sogar noch etwas größer zu wählen, Wohl Technik einfacher zu erlernen. Als zweite
Möglichkeit (wenn man nicht immer rechnen möchte) dienlich die praktische Lösung: Stock und Unterarm sollten bei fest
umfassten Griff und gerade aufgestelltem Stock einen leicht größeren Winkel als 90° ergeben. Stocklängen trotz gleicher Körpergröße optimal versorgt.
Bei dieser Art der individuellen Stocklängenzuweisung
werden auch verschiedene Menschentypen (z.B.
4. Wie viel sollten Nordic Walking Stöcke kosten? Der Preis variiert
abhängig deren Qualität des Schafts, vom Material beziehungsweise dem Carbonanteil
als auch vom Schlaufensystem. Die Preisspanne für einen Qualitätsstock bewegt sich zwischen 40€ und 100€.
Gelegenheitswalker bekommen zwischen 40-50€ das beste Preis-Leistungs-Verhältnis, Walker die ihre Stöcke öfter als 3mal diese Woche im Einsatz haben, sollten entsprechend nicht auf den Preis gucken.
Hey there! I know this is somewhat off topic
but I was wondering which blog platform are you using for this
website? I’m getting sick and tired of WordPress because I’ve
had issues with hackers and I’m looking at options for another platform.
I would be awesome if you could point me in the direction of a good
platform.
Your style is very unique compared to other folks I have read stuff from.
Thank you for posting when you have the opportunity, Guess
I will just book mark this page.
This is a very good tip especially to those fresh
to the blogosphere. Simple but very accurate information… Thank
you for sharing this one. A must read article!
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 a few pics to drive the message home
a little bit, but other than that, this is wonderful blog.
A great read. I will definitely be back.
Excellent blog post. I definitely love this website.
Stick with it!
I am extremely impressed with your writing talents
and also with the layout in your weblog. Is this a paid subject or did you customize it yourself?
Either way keep up the excellent high quality writing, it’s rare to look a nice blog
like this one nowadays..
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You definitely know what youre talking about, why waste your intelligence
on just posting videos to your weblog when you could be giving us something informative to read?
Greetings! Very helpful advice within this post!
It is the little changes that produce the largest changes.
Thanks for sharing!
As a Newbie, I am constantly exploring online for articles that can help me.
Thank you
Can I just say what a relief to discover someone who truly understands what they’re discussing on the web.
You actually 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
your story. I can’t believe you’re not more popular since you surely have the
gift.
I got this web site from my buddy who informed me concerning this web
site and now this time I am visiting this website and reading very informative posts at this time.
I’m gone to inform my little brother, that he should also visit this webpage on regular basis to get updated from
hottest reports.
This is a topic that is near to my heart… Best wishes!
Where are your contact details though?
I visited many blogs but the audio feature for audio songs current at
this website is actually wonderful.