JavaScript 陣列元素順序重新排序 sort()

JavaScript
Array Sort
METHODS

Array.sort()

陣列排序函數 sort() 方法,預設為 ASCII 字符順序進行「升序排列」。➤ ASCII 控制字符標準碼

var Brand = ["Oldsmobile", "Pontiac", "Buick", "Cadillac", "Holden", "Saturn", "GMC"];
Brand.sort()
Buick, Cadillac, GMC, Holden, Oldsmobile, Pontiac, Saturn

Brand.sort() //陣列以 ASCII 重新排序
Brand.reverse() //順序反轉(反轉元素的排列秩序)
Saturn, Pontiac, Oldsmobile, Holden, GMC, Cadillac, Buick

使用 charCodeAt 查詢第一個字的 ASCII 十進位編碼,查詢排列的順序。

for (let i = 0; i < Brand.length; i++) {
  console.log(Brand[i].substr(0, 1) + " = " + Brand[i].charCodeAt(0));
}
B = 66
C = 67
G = 71
H = 72
O = 79
P = 80
S = 83


可以依照「文字的長度」來排列順序。

Brand.sort(function (a, b) {
  return a.length - b.length
});
GMC, Buick, Holden, Saturn, Pontiac, Cadillac, Oldsmobile 


數字陣列.sort(compare function 函數)

var InStock = [12, 3, 5, 53, 12, 53, 47];
InStock.sort()
12, 12, 3, 47, 5, 53, 53

數字陣列直接於 .sort() 預設下則是以 ASCII 字符順序。需要使用 function 函數來進行比對。

InStock.sort(function (a, b) {
  return a - b
});
3, 5, 12, 12, 47, 53, 53

InStock.sort(function (a, b) {
  return b - a //順序反轉
});
53, 53, 47, 12, 12, 5, 3

Array 陣列重複值的計算

刪除重複的元素使用 forEach 函數來進行。
針對 Array 陣列中的每個元素各呼叫 callbackfn 函式一次(以遞增索引順序)。

var InStock = [12, 3, 5, 53, 12, 53, 47];
var NewStock = [];
InStock.forEach(function (value) {
  if (NewStock.indexOf(value) == -1) {
    NewStock.push(value);
  }
});
3, 5, 12, 47, 53

var InStock = [12, 3, 5, 53, 12, 53, 47];
var result = InStock.reduce(function (obj, item) {
  obj[item] = 1;
  return obj;
}, {});
console.log(Object.keys(result));
Array(5) [ "3", "5", "12", "47", "53" ]


數字與字串混合排序

var mixArray = ["ABC", "BCD", 2, "abc", 13, 1, "bcd", 5];
mixArray.sort();
1, 13, 2, 5, ABC, BCD, abc, bcd

mixArray.sort(function (a, b) {
  return a - b
});
ABC, BCD, 1, 2, abc, 13, bcd, 5

mixArray.sort(function (a, b) {
  if (typeof a == "string" && typeof b == "string") {
    return a.localeCompare(b);
  }
  if (typeof a == "number" && typeof b == "string") {
    return -1;
  }
  if (typeof a == "string" && typeof b == "number") {
    return 1;
  }
  if (typeof a == "number" && typeof b == "number") {
    if (a > b) return 1;
    if (a == b) return 0;
    if (a < b) return -1;
  }
});
1, 2, 5, 13, abc, ABC, bcd, BCD

JavaScript Object 排序

var Vehicles = [
{InStock:12, Brand:"Oldsmobile", Year:"2006-2008"},
{InStock:3, Brand:"Pontiac", Year:"2008-2010"},
{InStock:5, Brand:"Buick", Year:"2009-2010"},
{InStock:53, Brand:"Cadillac", Year:"2011-2013"},
{InStock:12, Brand:"Holden", Year:"2004-2006"},
{InStock:53, Brand:"Saturn", Year:"2009-2013"},
{InStock:47, Brand:"GMC", Year:"2007-2008"}
];

列出目前的值

for (var i = 0, l = Vehicles.length; i < l; ++i) {
 document.write(Vehicles[i].InStock + ", " +
 Vehicles[i].Brand + ", " +
 Vehicles[i].Year + "<br/>");
}
12, Oldsmobile, 2006-2008
3, Pontiac, 2008-2010
5, Buick, 2009-2010
53, Cadillac, 2011-2013
12, Holden, 2004-2006
53, Saturn, 2009-2013
47, GMC, 2007-2008

依照 Brand 進行順序,使用物件中的成員名稱來排列。

Vehicles = Vehicles.sort(function (a, b) {
 return a.Brand > b.Brand ? 1 : -1;
});
5, Buick, 2009-2010
53, Cadillac, 2011-2013
47, GMC, 2007-2008
12, Holden, 2004-2006
12, Oldsmobile, 2006-2008
3, Pontiac, 2008-2010
53, Saturn, 2009-2013

依照 Year 進行順序(比較改用年份 a.Year < b.Year 由大到小)。

Vehicles = Vehicles.sort(function (a, b) {
 return a.Year < b.Year ? 1 : -1;
});
53, Cadillac, 2011-2013
53, Saturn, 2009-2013
5, Buick, 2009-2010
3, Pontiac, 2008-2010
47, GMC, 2007-2008
12, Oldsmobile, 2006-2008
12, Holden, 2004-2006


二維陣列

Object 排序如果使用的是陣列 index 順序,所以不正確。

Vehicles = Vehicles.sort(function (a, b) {
 return a[2] < b[2] ? 1 : -1;
});
12, Oldsmobile, 2006-2008
3, Pontiac, 2008-2010
5, Buick, 2009-2010
53, Cadillac, 2011-2013
12, Holden, 2004-2006
53, Saturn, 2009-2013
47, GMC, 2007-2008

資料結構需要為 Array 陣列

同樣依照 Year 進行順序但資料結構為「二維陣列」

var Vehicles = [
[12, "Oldsmobile", "2006-2008"],
[3, "Pontiac", "2008-2010"],
[5, "Buick", "2009-2010"],
[53, "Cadillac", "2011-2013"],
[12, "Holden", "2004-2006"],
[53, "Saturn", "2009-2013"],
[47, "GMC", "2007-2008"]
];
Vehicles = Vehicles.sort(function (a, b) {
 return a[2] - b[2];
});
12, Oldsmobile, 2006-2008
3, Pontiac, 2008-2010
5, Buick, 2009-2010
53, Cadillac, 2011-2013
12, Holden, 2004-2006
53, Saturn, 2009-2013
47, GMC, 2007-2008