我知道如何按数字或字母排序,不知道如何做到这两点。
sessionStorage.cart
==> "[
{"id":1, "number":1, "other_attributes":"other_values"},
{"id":2, "number":2, "other_attributes":"other_values"},
{"id":"processing", "other_attributes":"other_values"},
{"id":2, "number":1, "other_attributes":"other_values"},
{"id":"deposit", "other_attributes":"other_values"}
]"
我想按id
对购物车进行排序,其中适用以下内容:
id
为整数id
是一个字符串id
是一个整数,如果多个项目具有相同的整数,请按number
排序id
是一个字符串,按字母顺序排序上面示例的最终结果应该是(在JSON解析之后,并设置回sessionStorage
)
==> "[
{"id":1, "number":1, "other_attributes":"other_values"},
{"id":2, "number":1, "other_attributes":"other_values"},
{"id":2, "number":2, "other_attributes":"other_values"},
{"id":"deposit", "other_attributes":"other_values"},
{"id":"processing", "other_attributes":"other_values"},
]"
答案 0 :(得分:2)
您可以将 sort()
与自定义排序功能
var arr = [{
"id": 1,
"number": 1,
"other_attributes": "other_values"
}, {
"id": 2,
"number": 2,
"other_attributes": "other_values"
}, {
"id": "processing",
"other_attributes": "other_values"
}, {
"id": 2,
"number": 1,
"other_attributes": "other_values"
}, {
"id": "deposit",
"other_attributes": "other_values"
}];
arr = arr.sort(function(a, b) {
// check both id's are number
if (typeof a.id == 'number' && typeof b.id == 'number') {
// check both are equal
if (a.id == b.id)
// if equal sort based on `number`
return a.numer - b.number
// else sort based on id
return a.id - b.id
// check first one is number
} else if (typeof a.id == 'number')
// since number have higher sort order return -1
return -1;
// check second one is number
else if (typeof b.id == 'number')
// since string have lower sort order return 1
return 1;
// in case both are string
else {
// return based on string comaprison
if (a.id > b.id)
return 1;
else if (a.id < b.id)
return -1
return 0;
}
});
document.write('<pre>' + JSON.stringify(arr, null, 3) + '</pre>');
答案 1 :(得分:0)
您需要排序两次
<强>样本强>
var arr = [
{"id":1, "number":1, "other_attributes":"other_values"},
{"id":2, "number":2, "other_attributes":"other_values"},
{"id":"processing", "other_attributes":"other_values"},
{"id":2, "number":1, "other_attributes":"other_values"},
{"id":"deposit", "other_attributes":"other_values"}
]
arr.sort(function(a,b){ var numa = a.number || Number.MAX_VALUE; var numb = b.number || Number.MAX_VALUE; return numa - numb; });
arr.sort(function(a,b){ return a.id - b.id });
document.body.innerHTML += JSON.stringify(arr,1,4);
&#13;
答案 2 :(得分:0)
这应该有效
MacDonald
&#13;
答案 3 :(得分:0)
基本上与上面相同,但我喜欢缩进:)还利用了id只是一个字符串或一个整数来使用localeCompare的事实
var cart = [
{"id":1, "number":1, "other_attributes":"other_values"},
{"id":2, "number":2, "other_attributes":"other_values"},
{"id":"processing", "other_attributes":"other_values"},
{"id":2, "number":1, "other_attributes":"other_values"},
{"id":"deposit", "other_attributes":"other_values"}
];
function compareCart(a, b) {
if (typeof a.id == 'number') {
if (typeof b.id == 'number') {
if (a.id == b.id) {
return a.number - b.number;
}
return a.id - b.id;
}
return -1;
}
if (typeof b.id == 'number') {
return 1;
}
return a.id.localeCompare(b.id);
}
cart.sort(compareCart);
console.log(cart);
答案 4 :(得分:0)
您可以使用javascript sort
功能&amp; ternary operator
实现这一目标。这基本上是多级排序。
简要说明
如果我们考虑id
,我们将通过前两个id,如果它们不相等则进行比较。
在这里&#34;?&#34;是条件检查器,它将检查它们是否大于或小于彼此并返回1或-1。 1将返回升序&amp; -1将按降序返回。当两个id相等时,它将检查这种情况
a.number>b.number?1:a.number<b.number?-1:0
&#34; 0&#34;意味着如果两者相等则不会返回任何内容。如果number
相等,您可以通过扩展三元运算符将排序深度扩展到"other_attributes"
。
为了检查排序的基因统一性,我添加了更多的价值。
var a = [
{"id":1, "number":1, "other_attributes":"other_values"},
{"id":2, "number":3, "other_attributes":"other_values"},
{"id":2, "number":2, "other_attributes":"other_values"},
{"id":"processing", "other_attributes":"other_values"},
{"id":2, "number":1, "other_attributes":"other_values"},
{"id":"deposit", "other_attributes":"other_values"},
{"id":1, "number":10, "other_attributes":"other_values"}
]
var b = a.sort(function(a,b){
return a.id.toString() >b.id.toString() ?1:a.id.toString()<b.id.toString()?-1:a.number>b.number?1:a.number<b.number?-1:0
})
console.log(b)