-2

In this code, sorting is done according to last name only. What I need to do is sort last name as first level of sorting and first name as second level of sorting.

   public int compare(Object aO1, Object aO2) {
    SearchResultAccount acct1 = (SearchResultAccount) aO1;
    String lastName1 = acct1.getLastName();
    SearchResultAccount acct2 = (SearchResultAccount) aO2;
    String lastName2 = acct2.getLastName();

    return lastName1.compareTo(lastName2);
}

I want last name sorted as first level of sorting and first name as second level of sorting

Dino
  • 7,779
  • 12
  • 46
  • 85
  • question is not clear. please add input and expected output . – SSP Sep 25 '19 at 06:56
  • 3
    `list.sort(Comparator.comparing(Person::getLastName).thenComparing(Person::getFirstName))`. This has been asked a million times already. – JB Nizet Sep 25 '19 at 06:56

1 Answers1

0
public int compare(Object aO1, Object aO2)
{
    SearchResultAccount acct1 = (SearchResultAccount) aO1;
    SearchResultAccount acct2 = (SearchResultAccount) aO2;

    int lastNameComparision = acct1.getLastName().compareTo(acct2.getLastName());
    if (lastNameComparision != 0) {
        return lastNameComparision;
    } else {
        return acct1.getFirstName().compareTo(acct2.getFirstName());
    }     
}
Alexey Nikitin
  • 604
  • 7
  • 22