0 votes
1 view
in Java by (9.2k 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 (19k 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
0 votes
1 answer
asked Mar 2 in Java by sheela_singh (9.2k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...