Natural search the object array with numbers and text in Javascript -
i have array following, need apply natural sort on this, on text field.
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" }]
i have tried following things far :
you use string#localecompare
options
sensitivity
which differences in strings should lead non-zero result values. possible values are:
"base"
: strings differ in base letters compare unequal. examples:a ≠ b
,a = á
,a = a
."accent"
: strings differ in base letters or accents , other diacritic marks compare unequal. examples:a ≠ b
,a ≠ á
,a = a
."case"
: strings differ in base letters or case compare unequal. examples:a ≠ b
,a = á
,a ≠ a
."variant"
: strings differ in base letters, accents , other diacritic marks, or case compare unequal. other differences may taken consideration. examples:a ≠ b
,a ≠ á
,a ≠ a
.the default "variant" usage "sort"; it's locale dependent usage "search".
numeric
whether numeric collation should used, such "1" < "2" < "10". possible values
true
,false
; defaultfalse
. option can set through options property or through unicode extension key; if both provided,options
property takes precedence. implementations not required support 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; }
wiki
Comments
Post a Comment