I want to sort an ArrayList by a property. This is my code…
public class FishDB{
public static Object Fish;
public ArrayList<Fish> list = new ArrayList<Fish>();
public class Fish{
String name;
int length;
String LatinName;
//etc.
public Vis (String name) {
this.name = name;
}
}
public FishDB() {
Fish fish;
fish = new Fish("Shark");
fish.length = 200;
fish.LatinName = "Carcharodon Carcharias";
fish = new Fish("Rainbow Trout");
fish.length = 80;
fish.LatinName = "Oncorhynchus Mykiss";
//etc.
}
}
}
Now I want in want to sort this ArrayList by a property e.g the latinname in another activity. But I don’t know how to do that. Does anybody know how?
You need to implement a
Comparator, for instance:and then sort it like this: