javascript - Natural sorting object of indexed arrays -
i have structure, have no control over, , need sort clients. index of various arrays relate each other, i.e. client[0] relates suites[0]. can structure sorted in way ?
current structure :
'client' : [ bravo,alpha, 1 delta, 2 charlie], 'locations' : [ 2,1,3,4 ], 'suites' : [ b,a,c,d ], 'ids' : [ 16,18,25,65 ]
expected output:
'client' : [ 1 delta, 2 charlie, alpha,bravo ], 'locations' : [ 3,4,1,2 ], 'suites' : [ c,d,a,b ], 'ids' : [ 25,65,18,16 ]
you use sorting map , 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 object = { client: ['bravo', 'alpha', '1 delta', '2 charlie'], locations: [2, 1, 3, 4], suites: ['b', 'a', 'c', 'd'], ids: [16, 18, 25, 65] }, indices = object.client.map((_, i) => i); indices.sort((a, b) => object.client[a].localecompare(object.client[b], undefined, { numeric: true, sensitivity: 'base' })); object.keys(object).foreach(function (key) { object[key] = indices.map(i => object[key][i]); }); console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Comments
Post a Comment