1

How can I sort the following array, in a custom order, based on the prop2's last 3 letters that have to be in this order 'ABR', 'FDE', 'ZFR' and also in descending order 'ZFR', 'FDE', 'ARB' for another array.

Sample input:

const arr = [
  { prop1: 3, prop2: '3FDE' },
  { prop1: 4, prop2: '5ZFR' },
  { prop1: 5, prop2: '7ABR' }
]

Expected output:

const arr1 = [
  { prop1: 5, prop2: '7ABR' },
  { prop1: 3, prop2: '3FDE' },
  { prop1: 4, prop2: '5ZFR' }
]

and

const arr2 = [
  { prop1: 4, prop2: '5ZFR' },
  { prop1: 3, prop2: '3FDE' },
  { prop1: 5, prop2: '7ABR' }
]
ryy77
  • 1,134
  • 5
  • 20
  • 36
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – WilliamNHarvey Apr 12 '22 at 19:12
  • Does this answer your question? [javascript - sort javascript array of objects based on the value](https://stackoverflow.com/questions/32208242/javascript-sort-javascript-array-of-objects-based-on-the-value) – Raymond Chen Apr 12 '22 at 19:13
  • Yes, this will answer to my question – ryy77 Apr 13 '22 at 06:31

3 Answers3

1

const arr = [ { prop1: 3, prop2: '3FDE' }, { prop1: 4, prop2: '5ZFR' }, { 
prop1: 5, prop2: '7ABR' } ];

const _getLastThree = (str = '') => str.substr(str.length - 3);
const res = arr.sort(({ prop2: a }, { prop2: b }) => 
  _getLastThree(a).localeCompare(_getLastThree(b))
);

console.log(res);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

Just use .localeCompare() in combination with .slice():

const arr=[
{prop1:5, prop2:'7ABR'},
{prop1:3, prop2:'3FDE'},
{prop1:4, prop2:'5ZFR'},
]


var ascArr,dscArr;
console.log(ascArr=arr.slice(0).sort((a,b)=>a.prop2.slice(-3).localeCompare(b.prop2.slice(-3))));

// for descending order just do 
console.log(dscArr=arr.slice(0).sort((a,b)=>-a.prop2.slice(-3).localeCompare(b.prop2.slice(-3))));

// or reverse the resulting array with 
console.log(dscArr=ascArr.reverse());

Please note that the two slice() methods belong to different prototypes: .slice(0) is an Array method that essentially creates a flat copy of the original array while .slice(-3) is a String method that "shaves off" the last three characters of the string.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • Thank you very much! Do you know how to sort it also descending? In this order '5ZFR','3FDE','7ARB'? – ryy77 Apr 13 '22 at 06:14
1

You can use the Array#sort, String#substring or String#slice or String#substr and the String#localCompare methods as follows:

  • substring

const arr = [ {prop1:4, prop2:'5ZFR'}, {prop1:5, prop2:'7ABR'}, {prop1:3, prop2:'3FDE'} ];

const output = arr.sort(
   (a,b) => 
   a.prop2.substring(1).localeCompare( b.prop2.substring(1) )
);

console.log( output );

With slice or substr you can use a negative n to indicate the last n characters:

const arr = [ {prop1:4, prop2:'5ZFR'}, {prop1:5, prop2:'7ABR'}, {prop1:3, prop2:'3FDE'} ];

const output = arr.sort(
   (a,b) => 
   a.prop2.slice(-3).localeCompare( b.prop2.slice(-3) )
);

console.log( output );
PeterKA
  • 24,158
  • 5
  • 26
  • 48