use*_*082 5 javascript collator
我在任何地方都找不到使用collator.compare排序对象的任何示例。有人可以提供吗?到目前为止,我遇到的所有文档和示例都涉及到show array sorting,如下所示:
var myArray = ['1_Document', '11_Document', '2_Document'];
var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
myArray.sort(collator.compare)
Run Code Online (Sandbox Code Playgroud)
很高兴看到它如何对像
var objs = [{name: '1_Document', size: 40}, {name: '11_Document', size: 50}, {name: '2_Document', size: 60}];
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以使用Intl.Collator对对象数组进行排序,方法是将其包装collator.compare到传递对象引用作为参数的函数
var collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base'
});
var objs = [{
name: '1_Document',
size: 40
}, {
name: '11_Document',
size: 50
}, {
name: '2_Document',
size: 60
}];
objs.sort(function(a, b) {
return collator.compare(a.name, b.name)
});
console.log(objs);Run Code Online (Sandbox Code Playgroud)