Sorting arraylist in alphabetical order (case insensitive)
The simplest thing to do is:
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Based on the above mentioned answers, I managed to compare my custom Class Objects like this:
ArrayList<Item> itemList = new ArrayList<>();
...
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item item, Item t1) {
String s1 = item.getTitle();
String s2 = t1.getTitle();
return s1.compareToIgnoreCase(s2);
}
});
try this code
Collections.sort(yourarraylist, new SortBasedOnName());
import java.util.Comparator;
import com.RealHelp.objects.FBFriends_Obj;
import com.RealHelp.ui.importFBContacts;
public class SortBasedOnName implements Comparator
{
public int compare(Object o1, Object o2)
{
FBFriends_Obj dd1 = (FBFriends_Obj)o1;// where FBFriends_Obj is your object class
FBFriends_Obj dd2 = (FBFriends_Obj)o2;
return dd1.uname.compareToIgnoreCase(dd2.uname);//where uname is field name
}
}
Custom Comparator
should help
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
Or if you are using Java 8:
list.sort(String::compareToIgnoreCase);