-6

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.

Community
  • 1
  • 1
Víctor Martínez
  • 854
  • 2
  • 11
  • 29
  • *"How could I achieve that in Java (Android)?"* By doing research, searching here on SO, reviewing the JavaDoc of the JDK, and taking a stab at it. *If* you do all that and run into a *specific* problem, post a question with your code and a description of that specific problem. People will be glad to help. – T.J. Crowder Aug 30 '17 at 09:05
  • Aha. But, in fact, THIS is a specific problem. I simplified it A LOT in order to make it straightforward. I have made previous research and I didn't find a solution. – Víctor Martínez Aug 30 '17 at 09:08
  • 1
    Oh pull the other one, it has legs on. You can't find anything on the entire web about how to sort an array in Java? Nonsense. – T.J. Crowder Aug 30 '17 at 09:09
  • Try this: https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – Krupa Kakkad Aug 30 '17 at 09:32
  • Duplicate of https://stackoverflow.com/questions/12449766/java-sorting-sort-an-array-of-objects-by-property-object-not-allowed-to-use-co, https://stackoverflow.com/questions/5245093/using-comparator-to-make-custom-sort, and probably a dozen others. – T.J. Crowder Aug 30 '17 at 09:39

2 Answers2

2

For Java 8 use a lambda expression and the Arrays sort function:

Users[] someUsers = {user1, user2, user3};
Arrays.sort(someUsers, (a,b) -> a.firstName.compareTo(b.firstName));

For previous versions use a Collection Comparator:

Users[] someUsers = {user1, user2, user3};
List<Users> userList = new ArrayList<Users>(Arrays.asList(someUsers));
Comparator<Users> comparator = new Comparator<Users>() {
    @Override
    public int compare(Users left, Users right) {
        return left.firstName.compareTo(right.firstName);
    }
};
Collections.sort(userList, comparator);
davidethell
  • 11,708
  • 6
  • 43
  • 63
  • It seems that I cannot use lambda expression because I am not using Java 8 in my Android Studio project (lambda expressions are not supported at this language level)... Is there anther way to achieve it? – Víctor Martínez Aug 30 '17 at 09:16
  • I have updated the answer to include using a Comparator for older versions of Java – davidethell Aug 30 '17 at 09:37
1

You can use Arrays.sort on an array to sort it. However, that method has no way of knowing how to sort Users. To make that happen, you need to implement the Comparable interface and then have the compareTo method delegate the comparison to the firstName field:

public static class User implements Comparable<User>{
  String firstName;
  public User(String firstName){
    this.firstName = firstName; 
  }
  @Override
  public int compareTo(User o) {
    return firstName.compareTo(o.firstName);
  }
}

Now you can simply sort the array using Arrays.sort.

f1sh
  • 11,489
  • 3
  • 25
  • 51