How to sort ArrayList<Object> in Ascending order android
Your approach was right. Make your Comparator
inner like in the following example (or you can create a new class instead):
ArrayList<Model> modelList = new ArrayList<>();
modelList.add(new Model("chandru"));
modelList.add(new Model("mani"));
modelList.add(new Model("vivek"));
modelList.add(new Model("david"));
Collections.sort(modelList, new Comparator<Model>() {
@Override
public int compare(Model lhs, Model rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
output:
chandru
david
mani
vivek
Collections.sort(actorsList, new Comparator<Actors>() {
@Override
public int compare(Actors lhs, Actors rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});