Say I have a class that looks like this (get/set omited):
class InfoClass{
String name;
String place;
double distance;
}
And I create an array of my class from within my main activity that looks like this:
InfoClass[3] myInfoClass;
myInfoClass[0].name = "venue one";
myInfoClass[0].place = "place one";
myInfoClass[0].distance = 11.23234;
myInfoClass[1].name = "venue two";
myInfoClass[1].place = "place two";
myInfoClass[1].distance = 9.2345643;
myInfoClass[2].name = "venue three";
myInfoClass[2].place = "place three";
myInfoClass[2].distance = 5.23432;
How can I sort my array (myInfoClass[]) so that it is ordered by the distance member?
i.e in the above example the array would be reversed because element [2] has the smallest distance and element [0] has the greatest distance?
Is there some function I can add to my class to do this or some other way?
Modify your class and implement
Comparableinterface if you don’t want to useComparatorits also preferable when by default you want to provide sorting toarray/collectionof your objects then go forComparableand then you can sort them