I need to alphabetically sort an array User[] contactList
(composed by instances of a custom class User
) by a built-in String attribute. I mean, for example, my class user is:
public class User{
String firstName;
public User(String firstName){
this.firstName = firstName;
}
}
If my array contactList
is composed by the following items:
contactList[0] has firstName = "Maria"
contactList[1] has firstName = "Andrew"
contactList[2] has firstName = "Joey"
I want to sort it alphabetically by their firstName
and thus, the result should be:
sortedList[0] should have firstName = "Andrew"
sortedList[1] should have firstName = "Joey"
sortedList[2] should have firstName = "Maria"
How could I achieve that in Java (Android)?
Edit 1: I cannot use lambda expressions in my project because I am using Java 7 in Android Studio.
Edit 2: I also cannot modify the User
class. I simplified it in this question, but that class is a built-in class of an API.