0

I am trying to sort an array of objects by attribute inside object and js is not sorting it well. This is the code that I used and the array that I am trying to sort:

const json = [ 
  { code: '40828',
    distance: { text: '2.3 km', value: 2309 },
    duration: { text: '14 mins', value: 831 },
  },
  { code: '43343',
    distance: { text: '35.4 km', value: 35432 },
    duration: { text: '1 hour 27 mins', value: 5242 },
  },
  { code: '40539',
    distance: { text: '26.1 km', value: 26134 },
    duration: { text: '1 hour 38 mins', value: 5885 },
  },
  { code: '4840',
    distance: { text: '2.3 km', value: 2340 },
    duration: { text: '17 mins', value: 1044 },
  },
  { code: '32725',
    distance: { text: '6.2 km', value: 6218 },
    duration: { text: '34 mins', value: 2011 },
  },
  { code: '39608',
    distance: { text: '3.6 km', value: 3620 },
    duration: { text: '22 mins', value: 1299 },
  },
  { code: '50913',
    distance: { text: '4.7 km', value: 4707 },
    duration: { text: '35 mins', value: 2112 },
  },
  { code: '43879',
    distance: { text: '10.1 km', value: 10065 },
    duration: { text: '49 mins', value: 2938 },
  },
  { code: '35606',
    distance: { text: '3.0 km', value: 2965 },
    duration: { text: '19 mins', value: 1168 },
  },
  { code: '4377',
    distance: { text: '4.5 km', value: 4524 },
    duration: { text: '24 mins', value: 1439 },
  },
  { code: '41519',
    distance: { text: '23.9 km', value: 23940 },
    duration: { text: '1 hour 5 mins', value: 3872 },
  }
]

console.log(json.sort((a, b) => a.duration.value > b.duration.value));

I am trying to sort it by distance.value and I am getting this result:

[ { code: '39608',
    distance: { text: '3.6 km', value: 3620 },
    duration: { text: '22 mins', value: 1299 } },
  { code: '40828',
    distance: { text: '2.3 km', value: 2309 },
    duration: { text: '14 mins', value: 831 } },
  { code: '4840',
    distance: { text: '2.3 km', value: 2340 },
    duration: { text: '17 mins', value: 1044 } },
  { code: '35606',
    distance: { text: '3.0 km', value: 2965 },
    duration: { text: '19 mins', value: 1168 } },
  { code: '4377',
    distance: { text: '4.5 km', value: 4524 },
    duration: { text: '24 mins', value: 1439 } },
  { code: '32725',
    distance: { text: '6.2 km', value: 6218 },
    duration: { text: '34 mins', value: 2011 } },
  { code: '50913',
    distance: { text: '4.7 km', value: 4707 },
    duration: { text: '35 mins', value: 2112 } },
  { code: '43879',
    distance: { text: '10.1 km', value: 10065 },
    duration: { text: '49 mins', value: 2938 } },
  { code: '41519',
    distance: { text: '23.9 km', value: 23940 },
    duration: { text: '1 hour 5 mins', value: 3872 } },
  { code: '43343',
    distance: { text: '35.4 km', value: 35432 },
    duration: { text: '1 hour 27 mins', value: 5242 } },
  { code: '40539',
    distance: { text: '26.1 km', value: 26134 },
    duration: { text: '1 hour 38 mins', value: 5885 } } ]

So everything in the result array looks good (and sorted well) except the first element, and I am trying to understand why? Value 831 should be first in an array but for some reason is second and first is 1299. Can anyone explain that maybe I am doing something wrong?

Thanks in advance

fr1sk
  • 204
  • 2
  • 13

2 Answers2

1

JS Sorts data based on ASCII. Please find below updated code.

console.log(json.sort((a, b) => a.duration.value - b.duration.value))

Sandip
  • 40
  • 3
1

You're close, but your sort is slightly off. The sort doesn't look for a boolean return type, it looks for integers. So you need to rework it like this: json.sort((a,b) => a.distance.value - b.distance.value);

ehutchllew
  • 940
  • 8
  • 14