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++){
  list.push({
    '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
});

補充:

上述的排序內容均以英文和數字為主,若是遇到中文可使用localeCompare進行,而排序的方式是漢語拼音順序,以下為範例:

var arr = ["二","五","四","一","三"];
//漢語拼音:一[yi], 二[er], 三[san], 四[si], 五[wu]
console.log("排序前:" + arr); // 排序前:二,五,四,一,三
arr.sort(function(a,b){
	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); //排序前:中文,英語,法國話,京片子,中國
arr.sort(function(a,b){
	return a.localeCompare(b, 'zh');
});
console.log("排序後:" + arr); //排序後:法國話,京片子,英語,中國,中文


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

You may also like...

7,207 Responses

  1. https://stromectoluk.com where to buy ivermectin pills for humans

  2. Luumop表示:

    baricitinib us – lisinopril cost lisinopril canada

  3. Richardmed表示:

    cheap medications best ed pill

  4. Awesome post. It’s so good to see someone taking the time to share this information

  5. JamesISodo表示:

    ed cures treat ed

  6. Kevinjails表示:

    https://pharmacyizi.com/# drugs to treat ed

  7. Steveheems表示:

    ed vacuum pump impotence treatment

  8. ArthurStada表示:

    https://pharmacyizi.com/# cheap online pharmacy

  9. Jefferycoist表示:

    https://pharmacyizi.com/# treatment with drugs

  10. Larryraity表示:

    https://pharmacyizi.com/# best medicine for ed

  11. Steveheems表示:

    medication for ed ed treatments that really work

  12. Larryraity表示:

    https://pharmacyizi.com/# cheap medications online

  13. aralen, buy chloroquine aralen help and adrenal insufficiency aralen who makes

  14. Douglasiteft表示:

    https://pharmacyizi.com/# comfortis for dogs without vet prescription

  15. Vqraad表示:

    plaquenil brand – cialis 10mg without prescription buy cenforce for sale

  16. JamesISodo表示:

    cheapest ed pills erectile dysfunction drug

  17. You have some helpful ideas! Maybe I should consider doing this by myself.

  18. Assume you are doing good linking to position you on the first pages of search engines.

  19. Jefferycoist表示:

    https://pharmacyizi.com/# online drugs

  20. A friend of mine advised me to review this site. And yes. it has some useful pieces of info and I enjoyed reading it.

  21. reformas表示:

    Makes sense to me.

  22. Phillipkek表示:

    stromectol for humans for sale stromectol 12 mg tablets

  23. Williambaw表示:

    stromectol stromectol 12 mg tablets

  24. Phillipkek表示:

    stromectol for humans for sale stromectol 3mg tablets

  25. paxil withdrawl symptoms how much is paxil in canada why does paxil make you gain weight what is common dose of paxil

  26. Joetepisn表示:

    language editing phd thesis
    dissertation proposal writing
    dissertation defense questions

  27. Robertobut表示:

    stromectol 3mg tablets ivermectin for demodex

  28. JamesFeelm表示:

    https://stromectoltrust.com/# stromectol 12 mg tablets

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。