Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:42:27+00:00 2026-06-09T02:42:27+00:00

I was looking around for a method to sort a list of objects based

  • 0

I was looking around for a method to sort a list of objects based on just one of its multiple fields (and i ended up just asking the question myself) but in my research, I came across this answer:

https://stackoverflow.com/a/1421537/1549672

I’m fairly new to java and this may be why, but I don’t quite understand this last method:

public static Comparator<Person> getComparator(final PersonComparator... multipleOptions) {
    return new Comparator<Person>() {
        public int compare(Person o1, Person o2) {
            for (PersonComparator option : multipleOptions) {
                int result = option.compare(o1, o2);
                if (result != 0) {
                    return result;
                }
            }
            return 0;
        }
    };
}

could someone please explain how it works…and what exactly it does?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:42:30+00:00Added an answer on June 9, 2026 at 2:42 am

    getComparator method will return comparator that will compare two Person objects based on comparators passed in its arguments. Each of PersonComparators is designed to compare Person by one of Person class field, for example

    • PersonComparator.ID_SORT will compare Person objects by id (0 < 1 < 2 < 3 < …)
    • PersonComparator.NAME_SORT will compare Person objects by name using natural (dictionary) order (“a” < “aa” < “ab” < “b”).

    If your Person class have more fields then you can add new comparator to enum PersonComparator.

    Also order of PersonComparators passed to getComparator method is important.

    For example, if you have Persons

    Person p1 = new Person(1,"Jack");
    Person p2 = new Person(2,"Jack");
    Person p3 = new Person(2,"Adam");
    

    and you will create comparator by

    getComparator(PersonComparator.ID_SORT,PersonComparator.NAME_SORT) 
    

    it will sort person first by their ids and in case ids ware equal then it will sort them by their name

    (1,"Jack") (2,"Adam") (2,"Jack")

    Also comparator created by

    getComparator(PersonComparator.NAME_SORT, PersonComparator.ID_SORT) 
    

    will firs compare names and only if names are equal it will compare ids of Persons, so you will get persons sorted this way

    (2,"Adam") (1,"Jack") (2,"Jack")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking around for about a week+ to implement a method I have
I've been looking around for a log4j viewer that allows for multiple log files
I've been looking around for a method to embed and call javascript functions in
I have been looking around for an answer to my question but couldn't find
Ok so I have been looking around at just about every option under the
Morning all (if its morning where you are) I have been looking around and
My question is essentially a simple one, though I'm looking for as in-depth an
I was looking around for the best method to do an online backup of
I have been looking around for a jQuery json method to pull in stats
So from looking around at examples and tutorials, it seems the most common method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.