0

I want to sort an array in ascending order based on two properties.

I have following data array which looks like

[
  {
    id: 1,
    name: 'ABP',
    code: 1460,
    subCode: '0010'
  },
  {
    id: 2,
    name: 'GKY',
    code: 1460,
    subCode: '0030'
  },
  {
    id: 3,
    name: 'CPT',
    code: 1410,
    subCode: '0070'
  },
  {
    id: 4,
    name: 'KLB',
    code: 1470,
    subCode: '0050'
  },
  {
    id: 5,
    name: 'POL',
    code: 1430,
    subCode: '0050'
  },
  {
    id: 6,
    name: 'FVB',
    code: 1410,
    subCode: '0050'
  },
]

I want to sort it like

[
  {
    id: 6,
    name: 'FVB',
    code: 1410,
    subCode: '0050'
  },
  {
    id: 3,
    name: 'CPT',
    code: 1410,
    subCode: '0070'
  },
  {
    id: 5,
    name: 'POL',
    code: 1430,
    subCode: '0050'
  },
  {
    id: 1,
    name: 'ABP',
    code: 1460,
    subCode: '0010'
  },
  {
    id: 2,
    name: 'GKY',
    code: 1460,
    subCode: '0030'
  },
  {
    id: 4,
    name: 'KLB',
    code: 1470,
    subCode: '0050'
  },
]

I want to sort the array in ascending order based on code property and if same code exist for multiple items then I want to sort it based on subCode of the code property.

Problem I am facing here is, subCode is in string and code is in number. I have tried using array.sort and also by parsing subCode in integer but it has returned me different number which I didn't understand.

  • Where is the code you used to parse your subcode coz there are different ways of parsing string to number... please paste that code – pavan kumar Nov 28 '19 at 06:41
  • what code u have written so far to achieve this ? – gkd Nov 28 '19 at 06:43
  • This might be what you are looking for: https://stackoverflow.com/q/6129952/12407377 – Arsi Nov 28 '19 at 06:43
  • [Try this](https://stackoverflow.com/a/18262813/8043806) but uses underscore.js - A javascript library which i think you would love. – Giddy Naya Nov 28 '19 at 06:44
  • @SriVenkataPavanKumarMHS I have tried using parseInt('0030') which returned me 30. Don't know why – Rishikesh Dehariya Nov 28 '19 at 06:45
  • @RishikeshDehariya, so what are you expecting it to return?... the parsInt() or Number Functions behave like that only as in '0000030' would also fetch you a 30, please specify wht output are you expecting – pavan kumar Nov 28 '19 at 06:55

2 Answers2

3

You could subtract the the code properties inside the compareFunction. If both a and b have the same code property, then the || operator will subtract the subCode property. The - operator will coerce the strings to numbers and it will return a numeric value.

const input=[{id:1,name:"ABP",code:1460,subCode:"0010"},{id:2,name:"GKY",code:1460,subCode:"0030"},{id:3,name:"CPT",code:1410,subCode:"0070"},{id:4,name:"KLB",code:1470,subCode:"0050"},{id:5,name:"POL",code:1430,subCode:"0050"},{id:6,name:"FVB",code:1410,subCode:"0050"},];

input.sort((a, b) => a.code - b.code || a.subCode - b.subCode)

console.log(input)
adiga
  • 34,372
  • 9
  • 61
  • 83
0

When you want to compare string value as number value (here is subCode), you will need to compare them by converting to number first

See below code.

Ary.sort(function (obj1, obj2) {
  if (obj1.code === obj2.code) {
    return Number(obj1.subCode) - Number(obj2.subCode)
  } else {
    return obj1.code - obj2.code
  }
})

I assume value of code will always be number.

gkd
  • 853
  • 1
  • 14
  • 31