我有一个包含多个对象的数组,例如:
var test = [
{size: "85A (UK 42A)"},
{size: "80A (UK 40A)"},
{size: "105F (UK 48F)"},
{size: "95E (UK 46E)"},
{size: "92C (UK 44C)"}
]
我想以大小订购,以便订单如下所示。
var test = [
{size: "80A (UK 40A)"}
{size: "85A (UK 42A)"},
{size: "92C (UK 44C)"}
{size: "95E (UK 46E)"},
{size: "105F (UK 48F)"}
]
我已经使用了这个,但这会返回下面的数组:
function sorting(json_object, key_to_sort_by) {
function sortByKey(a, b) {
var x = a[key_to_sort_by];
var y = b[key_to_sort_by];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
json_object.sort(sortByKey);
}
var test = [
{size: "105F (UK 48F)"},
{size: "80A (UK 40A)"}
{size: "85A (UK 42A)"},
{size: "92C (UK 44C)"}
{size: "95E (UK 46E)"}
]
答案 0 :(得分:2)
如果您希望使用数字后面的字符进行排序,也可以使用正则表达式来获取此值。
var test = [{ size: "105F (UK 48F)" }, { size: "80A (UK 40A)" }, { size: "85A (UK 2A)" }, { size: "92C (UK 44C)" }, { size: "80B (UK 40B)" }, { size: "80C (UK 40C)" }, { size: "95E (UK 46E)" }];
test.sort(function (a, b) {
function getV(s) { return s.match(/^(\d+)(\w+)/); }
var aa = getV(a.size),
bb = getV(b.size);
return aa[1] - bb[1] || aa[2].localeCompare(bb[2]);
});
console.log(test);
.as-console-wrapper { max-height: 100% !important; top: 0; }
对于大型数据集,您可以使用sorting with map,因为
数组中每个元素可以多次调用
compareFunction
。根据{{1}}的性质,这可能会产生很高的开销。compareFunction
做的工作越多,排序的元素就越多,可以考虑使用地图进行排序。我们的想法是遍历数组一次以提取用于排序到临时数组的实际值,对临时数组进行排序,然后遍历临时数组以实现正确的顺序。
compareFunction
// the array to be sorted
var list = [{ size: "105F (UK 48F)" }, { size: "80A (UK 40A)" }, { size: "85A (UK 2A)" }, { size: "92C (UK 44C)" }, { size: "80B (UK 40B)" }, { size: "80C (UK 40C)" }, { size: "95E (UK 46E)" }];
// temporary array holds objects with position and sort-value
var mapped = list.map(function(el, i) {
var temp = el.size.match(/^(\d+)(\w+)/);
return { index: i, number: +temp[1], string: temp[2] || '' };
});
// sorting the mapped array containing the reduced values
mapped.sort(function(a, b) {
return a.number - b.number || a.string.localeCompare(b.string);
});
// container for the resulting order
var result = mapped.map(function(el){
return list[el.index];
});
console.log(result);
答案 1 :(得分:1)
默认情况下,sort将按Unicode代码点对字符串进行排序。你想要隔离一些数字来排序,然后把它变成一个数字并用它来进行比较。这样的事情应该有用......
let sizes = [
{size: "85A (UK 42A)"},
{size: "80A (UK 40A)"},
{size: "105F (UK 48F)"},
{size: "95E (UK 46E)"},
{size: "92C (UK 44C)"},
{size: "80B (UK 40B)"},
];
sizes.sort(function(a, b) {
var sizeA = a.size.split(' ')[0];
var sizeB = b.size.split(' ')[0];
let aInt = parseInt(sizeA);
let bInt = parseInt(sizeB);
// If the same number, we can just compare the strings
// since the numeric characters will be equal.
if (aInt === bInt) {
return sizeA.localeCompare(sizeB);
}
// If not equal, compare the numbers.
return aInt - bInt;
});
// For snippet.
console.log(sizes);
编辑:假设如果数字组件相同,则您希望按字母排序。在这种情况下,我们只是比较字符串。
答案 2 :(得分:0)
var test = [
{size: "85A (UK 42A)"},
{size: "80A (UK 40A)"},
{size: "105F (UK 48F)"},
{size: "95E (UK 46E)"},
{size: "92C (UK 44C)"}
]
var sorted=test.sort(function(a, b) {
return parseInt(a['size'].split(' ')[0])-parseInt(b['size'].split('(')[0]);
})
console.log(sorted);