0

I need to put logic of first name sorting in this same compare method.

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);
}

It is sorting only by last name and I just want to sort according to first name as well in a same method.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Please [**search**](/search?q=%5Bjava%5D+sort+by+two+fields) before posting. More about searching [here](/help/searching). – T.J. Crowder Aug 28 '19 at 11:53
  • See the linked question's answers for details, but basically, if you want to sort by last name, and then for the same last name by first name, you check the result of `compareTo` on the last name and if it's `0`, return the result of comparing first names. (You can keep doing this for multiple fields. Just test the ones you want to sort on first, then the next, then the next, etc, returning the result of the first one that doesn't result in `0`.) – T.J. Crowder Aug 28 '19 at 11:58

0 Answers0