Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Can anyone tell me how to sort the below array with the objects from smallest distance to biggest distance ?

Object { distance=3388, duration="6 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

Object { distance=13564, duration="12 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

Object { distance=4046, duration="6 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

Object { distance=11970, duration="17 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}

1 Answer

0 votes
by (19.7k points)

Check the below code which uses Array's sort() method:

myArray.sort(function(a, b) {

    return a.distance - b.distance;

});

Interested in Java? Check out this Java tutorial by Intellipaat.   

Related questions

0 votes
1 answer
asked Apr 14, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Mar 2, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...