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 6332353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:14:54+00:00 2026-05-24T18:14:54+00:00

I have a list of java object that have 4 members. int id; String

  • 0

I have a list of java object that have 4 members.

int id;
String name;
String age;
int order;

I am using his code to sort object fields in ascending order according to if condition.

Collections.sort(list, new Comparator<com.MyObject>() {

          public int compare(com.MyObject object1, com.MyObject object2) {

            if(myString.equalsIgnoreCase("name")){
            return object1.getName().compareTo(object2.getName());

          }else if(myString.equalsIgnoreCase("age")){
            return object1.getAge().compareTo(object2.getAge());

          }else{
            return object1.getOrder() - object2.getOrder();
          }
      }
  });

I want to add another condition if(myString.equalsIgnoreCase("nameReverse")) If this will be the case the list should be sorted in descending order.

  • 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-05-24T18:14:55+00:00Added an answer on May 24, 2026 at 6:14 pm

    The Collections class has a reverseOrder method which returns a reverse Comparator for a given comparator. IMO, it would be more cleaner to not modify the Comparator if all you want to do is get a reverse Comparator.

    A bit of sample code:

    public class Person {
    
        public static void testComparator() {
            Person p1 = new Person("c");
            Person p2 = new Person("a");
            Person p3 = new Person("b");
            List<Person> persons = Arrays.asList(p1, p2, p3);
            Collections.sort(persons, PersonComparator.INSTANCE);
            System.out.println("After normal sorting: " + persons);
            Collections.sort(persons, Collections.reverseOrder(PersonComparator.INSTANCE));
            System.out.println("After reverse sorting: " + persons);
        }
    
        public Person(String name) { this.name = name; }
    
        public String name;
    
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("Person [name=").append(name).append("]");
            return builder.toString();
        }
    
    }
    
    class PersonComparator implements Comparator<Person> {
    
        public static final PersonComparator INSTANCE = new PersonComparator();
    
        @Override
        public int compare(Person o1, Person o2) {
            return o1.name.compareTo(o2.name);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of java object that have 4 members. int id; String
I have sorted list of strings that I move between php and java. to
I have a webservice in java that receives a list of information to be
If I have a list of elements I would like to sort, Java offers
I have inherited a bit of Java code that uses Hibernate. Some of the
Suppose that I have a list of business primary key numbers and a java
I'd like to have an object that implements both the Map and the List
Let's say I have a List object and an iterator for that list. Now
Say I have a list of names. case class Name(val first: String, val last:
I have a List of Java objects on my server which is sent to

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.