在 Javascript 中自然搜索包含数字和文本的对象数组
Natural search the object array with numbers and text in Javascript
我有如下数组,我需要它对此应用自然排序,仅在 text 字段上。
var data = [{
"text": "1001",
"value": "212121"
}, {
"text": "1002",
"value": "32435"
}, {
"text": "A101",
"value": "324124324"
}, {
"text": "A12",
"value": "567y54645"
}, {
"text": "A123",
"value": "534534"
}, {
"text": "A21",
"value": "34534534"
}, {
"text": "A210",
"value": "5345345"
}, {
"text": "A33",
"value": "234234234"
},
"text": "B2",
"value": "4234234"
}, {
"text": "D10000",
"value": "34234234"
}, {
"text": "EZH43NUT8SD",
"value": "534534534"
}, {
"text": "H287",
"value": "43435345"
}, {
"text": "Pkg test",
"value": "5345345"
}]
到目前为止,我已经尝试过以下操作:
AngularJS - Sorting ng-repeat on string with numbers in them
您可以将 String#localeCompare
与 options
一起使用
sensitivity
Which differences in the strings should lead to non-zero result values. Possible values are:
"base"
: Only strings that differ in base letters compare as unequal. Examples: a ≠ b
, a = á
, a = A
.
"accent"
: Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b
, a ≠ á
, a = A
.
"case"
: Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b
, a = á
, a ≠ A
.
"variant"
: Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b
, a ≠ á
, a ≠ A
.
The default is "variant" for usage "sort"; it's locale dependent for usage "search".
numeric
Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true
and false
; the default is false
. This option can be set through an options property or through a Unicode extension key; if both are provided, the options
property takes precedence. Implementations are not required to support this property.
var data = [{ text: "1001", value: "212121" }, { text: "1002", value: "32435" }, { text: "A101", value: "324124324" }, { text: "A12", value: "567y54645" }, { text: "A123", value: "534534" }, { text: "A21", value: "34534534" }, { text: "A210", value: "5345345" }, { text: "A33", value: "234234234" }, { text: "B2", value: "4234234" }, { text: "D10000", value: "34234234" }, { text: "EZH43NUT8SD", value: "534534534" }, { text: "H287", value: "43435345" }, { text: "Pkg test", value: "5345345" }, { text: "RRG46AXC3PO", value: "3434354" }, { text: "yoyo", value: "534534534" }];
data.sort(function (a,b) {
return a.text.localeCompare(b.text, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我有如下数组,我需要它对此应用自然排序,仅在 text 字段上。
var data = [{
"text": "1001",
"value": "212121"
}, {
"text": "1002",
"value": "32435"
}, {
"text": "A101",
"value": "324124324"
}, {
"text": "A12",
"value": "567y54645"
}, {
"text": "A123",
"value": "534534"
}, {
"text": "A21",
"value": "34534534"
}, {
"text": "A210",
"value": "5345345"
}, {
"text": "A33",
"value": "234234234"
},
"text": "B2",
"value": "4234234"
}, {
"text": "D10000",
"value": "34234234"
}, {
"text": "EZH43NUT8SD",
"value": "534534534"
}, {
"text": "H287",
"value": "43435345"
}, {
"text": "Pkg test",
"value": "5345345"
}]
到目前为止,我已经尝试过以下操作:
AngularJS - Sorting ng-repeat on string with numbers in them
您可以将 String#localeCompare
与 options
sensitivity
Which differences in the strings should lead to non-zero result values. Possible values are:
"base"
: Only strings that differ in base letters compare as unequal. Examples:a ≠ b
,a = á
,a = A
."accent"
: Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples:a ≠ b
,a ≠ á
,a = A
."case"
: Only strings that differ in base letters or case compare as unequal. Examples:a ≠ b
,a = á
,a ≠ A
."variant"
: Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples:a ≠ b
,a ≠ á
,a ≠ A
.The default is "variant" for usage "sort"; it's locale dependent for usage "search".
numeric
Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are
true
andfalse
; the default isfalse
. This option can be set through an options property or through a Unicode extension key; if both are provided, theoptions
property takes precedence. Implementations are not required to support this property.
var data = [{ text: "1001", value: "212121" }, { text: "1002", value: "32435" }, { text: "A101", value: "324124324" }, { text: "A12", value: "567y54645" }, { text: "A123", value: "534534" }, { text: "A21", value: "34534534" }, { text: "A210", value: "5345345" }, { text: "A33", value: "234234234" }, { text: "B2", value: "4234234" }, { text: "D10000", value: "34234234" }, { text: "EZH43NUT8SD", value: "534534534" }, { text: "H287", value: "43435345" }, { text: "Pkg test", value: "5345345" }, { text: "RRG46AXC3PO", value: "3434354" }, { text: "yoyo", value: "534534534" }];
data.sort(function (a,b) {
return a.text.localeCompare(b.text, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }