我正在嘗試按名稱欄位對物件陣列進行排序,但排序錯誤。
我得到的順序是 [1, 10, 11, 2, 20, 21, 3]
而不是 [1, 2, 3, .... 10, 11, 20, 21]
它正在排序,但將 10 放在 2 前面
這是我目前正在使用的代碼。
const arr = [
{
"name": "Action 10",
"color": "transparent",
"type": "components"
},
{
"name": "Action 11",
"color": "transparent",
"type": "components"
},
{
"name": "Action 2",
"color": "transparent",
"type": "components"
},
{
"name": "Action 20",
"color": "transparent",
"type": "components"
},
{
"name": "Action 21",
"color": "transparent",
"type": "components"
},
{
"name": "Action 3",
"color": "transparent",
"type": "components"
},
{
"name": "Action 4",
"color": "transparent",
"type": "components"
},
{
"name": "Action 5",
"color": "transparent",
"type": "components"
},
{
"name": "Action 6",
"color": "transparent",
"type": "components"
},
{
"name": "Action 1",
"color": "transparent",
"type": "components"
}
]
function sorter(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
}
console.log(arr.sort(sorter));
uj5u.com熱心網友回復:
可以嘗試一下numeric排序選項...String.prototype.localeCompare
const arr = [{
name: "Action 10", color: "transparent", type: "components"
}, {
name: "Action 11", color: "transparent", type: "components"
}, {
name: "Action 2", color: "transparent", type: "components"
}, {
name: "Action 20", color: "transparent", type: "components"
}, {
name: "Action 21", color: "transparent", type: "components"
}, {
name: "Action 3", color: "transparent", type: "components"
}, {
name: "Action 4", color: "transparent", type: "components"
}, {
name: "Action 5", color: "transparent", type: "components"
}, {
name: "Action 6", color: "transparent", type: "components"
}, {
name: "Action 1", color: "transparent", type: "components"
}];
function sorter(a, b) {
return a.name.localeCompare(b.name, undefined, { numeric: true });
}
console.log(arr.sort(sorter));
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
排序是正確的。字串“Action 10”對大于Action 1和小于進行排序Action 2,因為字串比較是在逐個字符的基礎上進行的。
字串比較的演算法可以在這里找到:https ://262.ecma-international.org/5.1/#sec-11.8.5
[如果] px 和 py 都是字串
如果py是px的前綴,則回傳
false. (如果q可以是連接p和其他字串r的結果,則字串值p是字串值q的前綴。請注意,任何字串都是其自身的前綴,因為r可能是空字串。)如果px是py的前綴,則回傳
true.令k為最小的非負整數,使得px中位置k處的字符與py中位置k處的字符不同。(必須有這樣的k,因為 String 都不是另一個的前綴。)
令m為整數,它是px中位置k處字符的代碼單元值。
令n為整數,它是py中位置k處的字符的代碼單元值。
如果m < n,則回傳
true。否則,回傳false。
或者,作為一個 javascript 函式,運算式str1 < str2被評估,就好像這個函式被呼叫了(不是任何理智的人都會按照規范中描述的那樣實作演算法):
function lt( px, py ) {
const prefixOf = (x,y) => x.slice(0,y.length) === y;
if ( prefixOf(px,py) ) return false;
if ( prefixOf(py,px) ) return true;
let k = 0 ;
while ( px[k] === py[k] ) {
k;
}
m = px.charCodeAt(k);
n = py.charCodeAt(k);
return m < n ? true : false;
}
如果要根據 的語意對事物進行排序,則name需要將字串劃分為其非數欄位和數欄位的串列,將數字位轉換為數字,然后按從左到右的順序比較段正確的。
uj5u.com熱心網友回復:
您可以使用正則運算式:
const sorter = (a, b) => a.name.match(/\d /) - b.name.match(/\d /);
演示
const arr = [{"name": "Action 10","color": "transparent","type": "components"}, {"name": "Action 11","color": "transparent","type": "components"}, {"name": "Action 2","color": "transparent","type": "components"}, {"name": "Action 20","color": "transparent","type": "components"}, {"name": "Action 21","color": "transparent","type": "components"}, {"name": "Action 3","color": "transparent","type": "components"}, {"name": "Action 4","color": "transparent","type": "components"}, {"name": "Action 5","color": "transparent","type": "components"}, {"name": "Action 6","color": "transparent","type": "components"}, {"name": "Action 1","color": "transparent","type": "components"}]
function sorter(a, b) {
return a.name.match(/\d /) - b.name.match(/\d /);
}
console.log(arr.sort(sorter));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/454364.html
標籤:javascript 数组
