1

I have this structure, which I have no control over, and need to do a sort by the clients. The index of the various arrays relate to each other, i.e. client[0] relates to suites[0]. Can this structure be sorted in this 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 ]
chip
  • 599
  • 1
  • 9
  • 20
  • Can you show what you've tried? – Alex Huszagh Jul 25 '17 at 18:42
  • what do you mean with ***natural sorting***? – Nina Scholz Jul 25 '17 at 18:43
  • Are `locations`, `suites`, and `ids` just dependent and are in the same locations as `client`? Also how you want to sort this needs to be more clear, do you want to sort this base on ASCII values? – Spencer Wieczorek Jul 25 '17 at 18:44
  • Thanks for the down votes, anyone care to share insight into why ? I've searched extensively on stack overflow and can't seem to find anyone doing this with this sort of structure. – chip Jul 25 '17 at 18:47
  • @AlexanderHuszagh, I tried a bunch of examples for different structures before realising they are not going to work on this, and I'm not quite sure where to start with this, I struggled to find any examples or tutorials of similar sorting. – chip Jul 25 '17 at 18:47
  • 1
    @NinaScholz https://en.wikipedia.org/wiki/Natural_sort_order – chip Jul 25 '17 at 18:47
  • @SpencerWieczorek yes, they are in the same index. i.e. - Bravo - 2 - B -16 all relate. – chip Jul 25 '17 at 18:47
  • @chip This basically just asking how to natural sort on an array of strings, [see this question](https://stackoverflow.com/questions/2802341/javascript-natural-sort-of-alphanumerical-strings) and [this one](https://stackoverflow.com/questions/51165/how-to-sort-strings-in-javascript). – Spencer Wieczorek Jul 25 '17 at 18:59

3 Answers3

4

You could use sorting with map and String#localeCompare with 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 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; }
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

The cleanest approach would probably be to first convert the input into a data structure more suited for the task, and then sort that.

E.g.,

  1. Convert to input to an array of objects,
  2. Sort the array by the right object attribute,
  3. (If required) convert the data back to the original structure.

While this may have a slightly negative impact and performance or memory, depending on the size of your data set this may be negligible compared to the likely more readable, less complicated implementation.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

let d = {
'client':['Bravo','Alpha','1 Delta','2 Charlie'],
'locations':[2,1,3,4],
'suites':['B','A','C','D'],
'ids':[16,18,25,65]
};
let mapped = d.client.map(function(el, i) {
  return { index: i, value: el };
})
mapped.sort(function(a, b) {
  return +(a.value > b.value) || +(a.value === b.value) - 1;
})
let cr = mapped.map(function(el){
  return d.client[el.index];
});
let lr = mapped.map(function(el){
  return d.locations[el.index];
});
let sr = mapped.map(function(el){
  return d.suites[el.index];
});
let ir = mapped.map(function(el){
  return d.ids[el.index];
});  
d.client =cr;
d.locations = lr;
d.suites = sr;
d.ids = ir;
console.log(d);
JGFMK
  • 8,425
  • 4
  • 58
  • 92