This is a sample array with 8 objects inside. Now, I want to sort them in ascending order. And I get the result perfectly in console.
const array =
[{"lead_location":"a"},{"lead_location":"rta"},
{"lead_location":"van"},{"lead_location":"svan"},
{"lead_location":"avan"},{"lead_location":"yvan"},
{"lead_location":"ab"},{"lead_location":"rta"}];
var sort = array.sort((a, b) => (a.lead_location) > (b.lead_location));
console.log(sort); // this array is sorted perfectly
However, If i add more objects in the array (13 in the next example), the array is not sorted well. I am using the same sort method as above. Second array is shown below :
const array =
[{"lead_location":"a"},{"lead_location":"rta"},
{"lead_location":"van"},{"lead_location":"svan"},
{"lead_location":"avan"},{"lead_location":"yvan"},
{"lead_location":"a"},{"lead_location":"rta"},
{"lead_location":"van"},{"lead_location":"svan"},
{"lead_location":"avan"},{"lead_location":"ab"},
{"lead_location":"rta"}];
var sort = array.sort((a, b) => (a.lead_location) > (b.lead_location));
console.log(sort); // this array is not sorted well
I am very confused, how come changing the size of the array will show different result? Anyone can spot my mistake?