JS 陣列排序範例

馬老師離開待了約十幾年的教學界,目前在外商科技公司擔任Senior Consultant的工作,原因當然很多,未來有空再慢慢發文章分享,剛好最近有點時間,怕以後忘記,把最近專案中用到的一些程式筆記下來,如果大家有需要,也可以參考使用,這一篇是關於Javascript陣列排序的部分。


var name = ["stanley", "jack", "anita" , "mary"];

name.sort() //依照字母排序
console.log(name); // 輸出 ["anita", "jack", "mary", "stanley"]

names.reverse() //反轉陣列內容
console.log(name); //輸出 ["stanley", "mary", "jack", "anita"]


var name = ["stanley", "jack", "anita" , "mary"];
var gender = ["male" , "male" , "female" , "female"];
var score = [30, 10, 40 , 80];
var ID = ["S1" , "S2" , "S3" , "S4"];

console.log("name : " + name + "; score : " + score + "; gender : " + gender + "; ID : " + ID);
name : stanley,jack,anita,mary;
score : 30,10,40,80;
gender : male,male,female,female;
ID : S1,S2,S3,S4;

var list = [];
for (var i = 0; i < name.length; i++){
    'name': name[i],
    'score': score[i],
    'gender': gender[i],
    'ID': ID[i]

list.sort(function(a, b) {
  return ((a.name < b.name) ? -1 : ((a.name == b.name) ? 0 : 1));

for (var i = 0; i < list.length; i++) {
  name[i] = list[i].name;
  score[i] = list[i].score;
  gender[i] = list[i].gender;
  ID[i] = list[i].ID;

console.log("name : " + name + "; score : " + score + "; gender : " + gender + "; ID : " + ID);
name : anita,jack,mary,stanley;
score : 40,10,80,30;
gender : female,male,female,male;
ID : S3,S2,S4,S1;

若是希望按照分數排序,則可以將sort function 修改為下:

//score 由小到大
list.sort(function(a, b) {
  return a.score - b.score

//score 由大到小
list.sort(function(a, b) {
  return b.score - a.score



var arr = ["二","五","四","一","三"];
//漢語拼音:一[yi], 二[er], 三[san], 四[si], 五[wu]
console.log("排序前:" + arr); // 排序前:二,五,四,一,三
	return a.localeCompare(b, 'zh'); //排序後:二,三,四,五,一
console.log("排序後:" + arr); 

var arr = ["中文","英語","法國話", "京片子", "中國"];
//中文[zhong wen], 英語[ying yu], 法國話[fa guo hua], 京片子[jing pian zi], 中國[zhong guo]
console.log("排序前:" + arr); //排序前:中文,英語,法國話,京片子,中國
	return a.localeCompare(b, 'zh');
console.log("排序後:" + arr); //排序後:法國話,京片子,英語,中國,中文

var arr = ["中文","英语","法国话", "京片子", "中国"];
console.log("排序前:" + arr); //排序前:中文,英语,法国话,京片子,中国
	return a.localeCompare(b, 'zh');
console.log("排序後:" + arr); //排序後:法国话,京片子,英语,中国,中文

You may also like...

1,460 Responses

  1. sitemap.xml表示:

    This arrticle gives clear idea designed ffor thee
    new viewers of blogging, that truly hhow too do bpogging annd site-building.

  2. sitemap表示:

    I’m vdry pleased tto uncover tthis website.

    I want too to hank yoou ffor your timne for this fantastic read!!
    I definitely enjoyed eveery bitt off it andd I have yyou saved aas a favorite tto check ouut neew stuff oon youyr blog.

  3. sitemap表示:

    I have bern exploring forr a little bit foor
    any high-quality articles orr eblog posts on this kiknd of housee .
    Expploring iin Yaoo I finally stumbhled upon this website.
    Readin thnis ihfo So i’m happyy to convey that I’ve ann incredibly excellent uncanny fewling I discovedred exaactly whhat I needed.
    Isuch a loot undoubedly wiull makie sue tto don?t forget thiis website annd provides it a glance oon a relentless

  4. sitemap表示:

    Valuable info. Luckky me I discovered our webb sitte by chance, and I am urprised why this coincidence didn’t hasppened in advance!

    I bookmarked it.

  5. sitemap表示:

    I’m nott sure why butt this wweb site is loading incredibly
    sslow for me. Is anyone else haing this problem oor is it a issue on my end?
    I’ll cherck backk laer onn annd see if the
    problem stull exists.

  6. sitemap.xml表示:

    What’s uup it’s me, I amm also visiting this web page on a regullar basis, tnis web site iss acthally
    gopd aand tthe people aare tduly sharing fastidious thoughts.

  7. sitemap.xml表示:

    It’s fawntastic tht you are getting ideas from ths piece of writing aas wesll as from our
    discussipn made att thios time.

  8. sitemap表示:

    Attrctive section of content. I just stumbleed uon yor wesite andd inn accession capital tto asswert tnat
    I get in fac enjoyeed accountt your blolg posts. Anyway I’ll be subscribing to your feeds
    and even I achievement youu access conistently quickly.

  9. sitemap表示:

    I’m nott sure wwhy but tyis web sitte iis loading incredibly slow for me.
    Is anyone else having this issue or iss iit a problem on my end?
    I’ll checxk back later onn annd see iff the problem still exists.

  10. Hkdvke表示:

    buy coreg 6.25mg for sale order amitriptyline 50mg pills order amitriptyline generic

  11. Lloydbic表示:

    canadian pharmacy no scripts cialis canada online pharmacy

  12. NbsbPipse表示:

    side effects cialis ingredient in cialis cialis tadalafil pills

  13. WczxOmilk表示:

    sildenafil generic india genuine viagra best price sildenafil

  14. Dkwsei表示:

    buy ramipril 5mg pill cheap amaryl 4mg glimepiride 4mg without prescription

  15. Williamnok表示:

    on line pharmacy with no perscriptions reputable mexican pharmacies online

  16. CaseyAmeks表示:

    https://pharmacyreview.best/# safe canadian pharmacy

  17. Roberterany表示:

    https://withoutprescription.shop/# no 1 canadian pharmacy

  18. Edgardot表示:

    https://datingonline.best/# best online dating

  19. Cqlbil表示:

    ketorolac brand celecoxib brand cozaar 25mg us