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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:46:51+00:00 2026-06-15T14:46:51+00:00

Possible Duplicate: Sorting Java objects using multiple keys I have an employee class. Based

  • 0

Possible Duplicate:
Sorting Java objects using multiple keys

I have an employee class. Based on the employee name I want to sort. If the employee names for two objects (i.e. two employees) are same then I want to sort it with respect to the employee age.

For sorting w.r.t the employee name, I implemented the COmparable interface and used this code:

@Override
public int compareTo(Employee o) {
    return name.compareTo(o.getName());
}

This bit of code is working fine for me. But I am not able to understand how to incorporate the sorting based on age when the name of two employee are same?

Do i need to use the Comparator for this? I am not sure how to achieve this.

Please help

  • 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-15T14:46:52+00:00Added an answer on June 15, 2026 at 2:46 pm

    If the names are the same (ie a result of 0) then you can then sort by age. If the names return a non zero result then just return that

    for example

    @Override
    public int compareTo(Employee o) {
        int i = name.compareTo(o.getName());
        if (i == 0) { //ie the names are the same
            return age.compareTo(o.getAge();
        }
        return i;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Sorting an array based on its value I want to sort an
Possible Duplicate: Sorting an ArrayList of Contacts based on name? I have a student
Possible Duplicate: Sorting a collection of objects How to sort the list in java
Possible Duplicate: Ruby: Sorting 2 arrays using values from one of them I have
Possible Duplicate: Sorting NSString values as if NSInteger using NSSortDescriptor I have an Array
Possible Duplicate: Sorting by Name (alphabetic) Say I have the code below. I have
Possible Duplicate: Sorting an array in descending order in Ruby I want to sort
Possible Duplicate: Sorting an IList in C# I have an IList<Keyword> where Keyword is
Possible Duplicate: C++ struct sorting Is it possible to sort a vector in C++
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can

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.