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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:22:24+00:00 2026-06-01T16:22:24+00:00

class Employee implements Comparable{ private String name; private String gender; Employee(String name, String gender)

  • 0
class Employee implements Comparable{
    private String name;
        private String gender;

        Employee(String name, String gender) {
            this.name = name;
            this.gender = gender;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGender() {
                return gender;
        }

        public void setGender(String gender) {
                this.gender = gender;
        }
    
        public String toString() {
            return this.getName() + " : " + this.getGender();
        }

        @Override
        public int compareTo(Object o) {
            int gendarComp=this.getGender().compareTo(((Employee)o).getGender());
            return (gendarComp!=0?gendarComp:this.name.compareTo(((Employee)o).getName()));
        }
    } 

    class EmployeeExec {
        public static void main(String[] args) {
            List allEmpls = new ArrayList();
            allEmpls.add(new Employee("vijay", "m"));
            allEmpls.add(new Employee("balaji", "m"));
            allEmpls.add(new Employee("shaifali", "f"));
            allEmpls.add(new Employee("archana", "f"));
            allEmpls.add(new Employee("alala", "m"));
            allEmpls.add(new Employee("kiran", "f"));

            sortEmployees(allEmpls);
        }

        public static void sortEmployees(List allEmpls) {
            Collections.sort(allEmpls);
            System.out.println(allEmpls);
        }
    }

Can anyone help me in understanding how it is working? what is the funda to sort? I kept some println statements to see, but I don’t really understand how it is getting called. I know its working with merge sort. But how?

  • 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-01T16:22:25+00:00Added an answer on June 1, 2026 at 4:22 pm

    The sorting is based on the return value of compareTo (slightly reformatted for readability):

    @Override
    public int compareTo(Object o) {
        int gendarComp = this.getGender().compareTo(((Employee)o).getGender());
        return (gendarComp != 0
                   ? gendarComp
                   : this.name.compareTo( ((Employee)o).getName() )
               );
    }
    

    The logic is apparently to compare values of getGender() and return that comparison unless they test equal. If they are equal, it returns the result of comparing names.

    As an aside: If this is your code, it would be better to use generics instead of raw types. Declare

    class Employee implements Comparable<Employee> {...
    

    Then declare the compareTo method with the signature:

    @Override
    public int compareTo(Employee o) {...
    

    and you can dispense with the casts inside the method (as well as improve type safety in client code).

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

Sidebar

Related Questions

class Employee { private String name; void setName(String n) { name = n; }
public class Employee implements java.io.Serializable { public String name; public int transient id; }
Consider the following inheritance: abstract class Employee { private string empID; private string empName;
This is what my data model classes look like: public class Employee { public
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
I have @manytomany relation in hibernate Like : Table Employee public class Employee implements
I have a table bean as : public class Employee implements Serializable { @Id
I have class Employee which is something like this. class Emp { int EmpID;
My Model Class as follows: public class Employee { public Guid ID { get;
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =

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.