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

  • Home
  • SEARCH
  • 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 9084183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:56:31+00:00 2026-06-16T20:56:31+00:00

Possible Duplicate: Sorting Java objects using multiple keys I can’t find any example of

  • 0

Possible Duplicate:
Sorting Java objects using multiple keys

I can’t find any example of using this method, all examples give the second parameter “null”.
I heard that this method used for sorting classes according to more than one criterion but no example where found.

public class Student implements Comparable<Student> {
String name;
int age;

public Student(String name, int age) {
    this.name = name;
    this.age = age;
}

@Override
public String toString() {
    return name + ":" + age;
}

@Override
public int compareTo(Student o) {
    Integer myAge = age;
    Integer oAge = o.age;
    return myAge.compareTo(oAge);
}

}

for this class if i want to sort a list of Student according to their names & ages how can i use the method Collections sort(List,Comparator)

  • 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-16T20:56:32+00:00Added an answer on June 16, 2026 at 8:56 pm

    Building upon your existing Student class, this is how I usually do it, especially if I need more than one comparator.

    public class Student implements Comparable<Student> {
    
        String name;
        int age;
    
        public Student(String name, int age) {
           this.name = name;
           this.age = age;
        }
    
        @Override
        public String toString() {
            return name + ":" + age;
        }
    
        @Override
        public int compareTo(Student o) {
            return Comparators.NAME.compare(this, o);
        }
    
    
        public static class Comparators {
    
            public static Comparator<Student> NAME = new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.name.compareTo(o2.name);
                }
            };
            public static Comparator<Student> AGE = new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.age - o2.age;
                }
            };
            public static Comparator<Student> NAMEANDAGE = new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    int i = o1.name.compareTo(o2.name);
                    if (i == 0) {
                        i = o1.age - o2.age;
                    }
                    return i;
                }
            };
        }
    }
    

    Usage:

    List<Student> studentList = new LinkedList<>();
    Collections.sort(studentList, Student.Comparators.AGE);
    

    EDIT

    Since the release of Java 8 the inner class Comparators may be greatly simplified using lambdas. Java 8 also introduces a new method for the Comparator object thenComparing, which removes the need for doing manual checking of each comparator when nesting them. Below is the Java 8 implementation of the Student.Comparators class with these changes taken into account.

    public static class Comparators {
        public static final Comparator<Student> NAME = (Student o1, Student o2) -> o1.name.compareTo(o2.name);
        public static final Comparator<Student> AGE = (Student o1, Student o2) -> Integer.compare(o1.age, o2.age);
        public static final Comparator<Student> NAMEANDAGE = (Student o1, Student o2) -> NAME.thenComparing(AGE).compare(o1, o2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sorting Java objects using multiple keys I have an employee class. Based
Possible Duplicate: jQuery tablesorter - Not sorting column with formatted currency value This http://tablesorter.com/docs/example-meta-parsers.html
Possible Duplicate: Sorting a collection of objects How to sort the list in java
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: About python’s built in sort() method Which of the sorting algorithms does
Possible Duplicate: Sorting objects in an array by a field value in JavaScript var
Possible Duplicate: Sorting objects in an array by a field value in JavaScript How
Possible Duplicate: problem sorting using member function as comparator Is it possible to use

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.