Baseline Widely available
sort()
ë©ìëë ë°°ì´ì ìì를 ì ì í ìì¹ì ì ë ¬í í ê·¸ ë°°ì´ì ë°íí©ëë¤. ì ë ¬ì stable sortê° ìë ì ììµëë¤. 기본 ì ë ¬ ììë 문ìì´ì ì ëì½ë ì½ë í¬ì¸í¸ë¥¼ ë°ë¦
ëë¤.
ì ë ¬ ìëì ë³µì¡ëë ê° êµ¬íë°©ìì ë°ë¼ ë¤ë¥¼ ì ììµëë¤.
ìëí´ ë³´ê¸°const months = ["March", "Jan", "Feb", "Dec"];
months.sort();
console.log(months);
// Expected output: Array ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// Expected output: Array [1, 100000, 21, 30, 4]
구문
arr.sort([compareFunction]);
매ê°ë³ì
compareFunction
Optional
ì ë ¬ ìì를 ì ìíë í¨ì. ìëµíë©´ ë°°ì´ì ê° ììì 문ìì´ ë³íì ë°ë¼ ê° ë¬¸ìì ì ë ì½ë ì½ë í¬ì¸í¸ ê°ì ë°ë¼ ì ë ¬ë©ëë¤.
ì ë ¬í ë°°ì´. ì ë°°ì´ì´ ì ë ¬ëë ê²ì ì ìíì¸ì. ë³µì¬ë³¸ì´ ë§ë¤ì´ì§ë ê²ì´ ìëëë¤.
ì¤ëªcompareFunction
ì´ ì ê³µëì§ ìì¼ë©´ ìì를 문ìì´ë¡ ë³ííê³ ì ë ì½ë ì½ë í¬ì¸í¸ ììë¡ ë¬¸ìì´ì ë¹êµíì¬ ì ë ¬ë©ëë¤. ì를 ë¤ì´ "ë°ëë"ë "체리"ìììµëë¤. ì«ì ì ë ¬ììë 9ê° 80ë³´ë¤ ìì ì¤ì§ë§ ì«ìë 문ìì´ë¡ ë³íë기 ë문ì "80"ì ì ë ì½ë ìììì "9"ìììµëë¤.
compareFunction
ì´ ì ê³µëë©´ ë°°ì´ ììë compare í¨ìì ë°í ê°ì ë°ë¼ ì ë ¬ë©ëë¤. aì bê° ë¹êµëë ë ììë¼ë©´,
compareFunction(a, b)
ì´ 0ë³´ë¤ ìì ê²½ì° a를 bë³´ë¤ ë®ì ìì¸ì¼ë¡ ì ë ¬í©ëë¤. ì¦, aê° ë¨¼ì ìµëë¤.compareFunction(a, b)
ì´ 0ì ë°ííë©´ aì b를 ìë¡ì ëí´ ë³ê²½íì§ ìê³ ëª¨ë ë¤ë¥¸ ììì ëí´ ì ë ¬í©ëë¤. ì°¸ê³ : ECMAscript íì¤ì ì´ë¬í ëìì ë³´ì¥íì§ ìì¼ë¯ë¡ 모ë ë¸ë¼ì°ì (ì : Mozilla ë²ì ì ì ì´ë 2003 ë
ì´í ë²ì ì)ê° ì´ë¥¼ ì¡´ì¤íì§ë ììµëë¤.compareFunction(a, b)
ì´ 0ë³´ë¤ í° ê²½ì°, b를 aë³´ë¤ ë®ì ì¸ë±ì¤ë¡ ìí¸í©ëë¤.compareFunction(a, b)
ì ìì aì bì í¹ì ìì´ ë ê°ì ì¸ìë¡ ì£¼ì´ì§ ë íì ëì¼í ê°ì ë°íí´ì¼í©ëë¤. ì¼ì¹íì§ ìë ê²°ê³¼ê° ë°íëë©´ ì ë ¬ ììë ì ìëì§ ììµëë¤.ë°ë¼ì compare í¨ìì íìì ë¤ìê³¼ ê°ìµëë¤.
function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
문ìì´ ëì ì«ì를 ë¹êµí기 ìí´ compare í¨ìë aìì b를 ëº ì ììµëë¤. ë¤ì í¨ìë ë°°ì´ì ì¤ë¦ì°¨ìì¼ë¡ ì ë ¬í©ëë¤ (Infinity ë° NaNì´ í¬í¨ëì´ ìì§ ìì ê²½ì°).
function compareNumbers(a, b) {
return a - b;
}
sort ë©ìëë í¨ìì (ë° í´ë¡ì )ì í¨ê» í¸ë¦¬íê² ì¬ì©í ì ììµëë¤.
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function (a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
ê°ì²´ë í´ë¹ ìì± ì¤ íëì ê°ì 기ì¤ì¼ë¡ ì ë ¬ í ì ììµëë¤.
var items = [
{ name: "Edward", value: 21 },
{ name: "Sharpe", value: 37 },
{ name: "And", value: 45 },
{ name: "The", value: -12 },
{ name: "Magnetic", value: 13 },
{ name: "Zeros", value: 37 },
];
// value 기ì¤ì¼ë¡ ì ë ¬
items.sort(function (a, b) {
if (a.value > b.value) {
return 1;
}
if (a.value < b.value) {
return -1;
}
// a must be equal to b
return 0;
});
// name 기ì¤ì¼ë¡ ì ë ¬
items.sort(function (a, b) {
var nameA = a.name.toUpperCase(); // ignore upper and lowercase
var nameB = b.name.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// ì´ë¦ì´ ê°ì ê²½ì°
return 0;
});
ìì ë°°ì´ ë§ë¤ê¸°, íì ë° ì ë ¬
ë¤ì ìì ììë ë¤ ê°ì ë°°ì´ì ë§ë¤ê³ ìë ë°°ì´ì íì í ë¤ì ì ë ¬ ë ë°°ì´ì íìí©ëë¤. ì«ì ë°°ì´ì ë¹êµ í¨ììì´ ì ë ¬ ë ë¤ì ë¹êµ í¨ìë¡ ì ë ¬ë©ëë¤.
var stringArray = ["Blue", "Humpback", "Beluga"];
var numericStringArray = ["80", "9", "700"];
var numberArray = [40, 1, 5, 200];
var mixedNumericArray = ["80", "9", "700", 40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
console.log("stringArray:", stringArray.join());
console.log("Sorted:", stringArray.sort());
console.log("numberArray:", numberArray.join());
console.log("Sorted without a compare function:", numberArray.sort());
console.log("Sorted with compareNumbers:", numberArray.sort(compareNumbers));
console.log("numericStringArray:", numericStringArray.join());
console.log("Sorted without a compare function:", numericStringArray.sort());
console.log(
"Sorted with compareNumbers:",
numericStringArray.sort(compareNumbers),
);
console.log("mixedNumericArray:", mixedNumericArray.join());
console.log("Sorted without a compare function:", mixedNumericArray.sort());
console.log(
"Sorted with compareNumbers:",
mixedNumericArray.sort(compareNumbers),
);
ì´ ìì ë ë¤ì ì¶ë ¥ì ìì±í©ëë¤. ê²°ê³¼ê° ë³´ì¬ ì£¼ë¯ì´ ë¹êµ í¨ìê° ì¬ì©ëë©´ ì«ìë ì«ì ëë ì«ì 문ìì´ì¸ì§ ì¬ë¶ì ê´ê³ìì´ ì¬ë°ë¥´ê² ì ë ¬ë©ëë¤.
stringArray: Blue,Humpback,Beluga Sorted: Beluga,Blue,Humpback numberArray: 40,1,5,200 Sorted without a compare function: 1,200,40,5 Sorted with compareNumbers: 1,5,40,200 numericStringArray: 80,9,700 Sorted without a compare function: 700,80,9 Sorted with compareNumbers: 9,80,700 mixedNumericArray: 80,9,700,40,1,5,200 Sorted without a compare function: 1,200,40,5,700,80,9 Sorted with compareNumbers: 1,5,9,40,80,200,700ë¹ ASCII 문ì ì ë ¬
ASCII ì´ì¸ì 문ì, ì¦ ì
ì¼í¸ ë¶í¸ê°ìë 문ì (e, é, è, a, ä ë±)ê°ìë 문ìì´ì ì ë ¬íë ¤ë©´ ìì´ê° ìë ë¤ë¥¸ ì¸ì´ì 문ìì´ì String.localeCompare
를 ì¬ì©íììì¤. ì´ í¨ìë í´ë¹ 문ì를 ë¹êµíì¬ ì¬ë°ë¥¸ ììë¡ ëíë¼ ì ììµëë¤.
var items = ["réservé", "premier", "cliché", "communiqué", "café", "adieu"];
items.sort(function (a, b) {
return a.localeCompare(b);
});
// items is ['adieu', 'café', 'cliché', 'communiqué', 'premier', 'réservé']
mapì ì¬ì©í ì ë ¬
compareFunction
ì ë°°ì´ ë´ì ììë§ë¤ ì¬ë¬ ë² í¸ì¶ë ì ììµëë¤. ì´ë¬í compareFunction
ì ì±ì§ì ë°ë¼, ëì ì¤ë²í¤ëê° ë°ìí ìë ììµëë¤. compareFunction
ì´ ë³µì¡í´ì§ê³ , ì ë ¬í ììê° ë§ìì§ ê²½ì°, mapì ì¬ì©í ì ë ¬ì ê³ ë ¤í´ë³´ë ê²ì´ ì¢ìµëë¤. ì´ ë°©ë²ì ìì ë°°ì´ì íë ë§ë¤ì´ì ì¬ê¸°ì ì¤ì ì ë ¬ì ì¬ì©í ê°ë§ì ë½ìì ë£ì´ì ì´ë¥¼ ì ë ¬íê³ , ê·¸ 결과를 ì´ì©í´ì ì¤ì ì ë ¬ì íë ê²ì
ëë¤.
// ìí¸ í ë°°ì´
var list = ["Delta", "alpha", "CHARLIE", "bravo"];
// ìì ë°°ì´ì ìì¹ ë° ì ë ¬ ê°ì´ìë ê°ì²´ë¥¼ ë³´ì í©ëë¤.
var mapped = list.map(function (el, i) {
return { index: i, value: el.toLowerCase() };
});
// ì¶ì ì¹ë¥¼ í¬í¨í 매í ë ë°°ì´ì ìí¸
mapped.sort(function (a, b) {
return +(a.value > b.value) || +(a.value === b.value) - 1;
});
// ê²°ê³¼ ìì를 ìí 컨í
ì´ë
var result = mapped.map(function (el) {
return list[el.index];
});
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4