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

The Archive Base Latest Questions

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

I was trying to develop an application in which the arraylist will be sorted

  • 0

I was trying to develop an application in which the arraylist will be sorted by first name and if the name of two persons are same then the further comparison will be done on the basis of their salary, for this an comparator has to be made please advise how to make that custom comparator..below is my program rite now which is only doing comparison the basis of salary rite now..!

  package saxbean;

class Emp //implements Comparable
{
      String name,job;
      int salary;
      public Emp(String n,String j,int sal)
      {
         name=n;
         job=j;
         salary=sal;
       }
      public void display()
      {
        System.out.println(name+"\t"+job+"\t"+salary);
       }
     public boolean equals(Object o)
      {

         Emp p=(Emp)o;
          return this.name.equals(p.name)&&this.job.equals(p.job) &&this.salary==p.salary;
       }
     public int hashCode()
       {
          return name.hashCode()+job.hashCode()+salary;
       }


      /* public int compareTo(Object o)
       {
          Emp e=(Emp)o;
          return this.name.compareTo(e.name);
           //return this.job.compareTo(e.job);
        //   return this.salary-e.salary;

        }*/
} 

MY Comprator is …

import java.util.Comparator;

    class SalaryComparator implements Comparator {
        public int compare(Object paramObject1, Object paramObject2) {
            Emp localEmp1 = (Emp) paramObject1;
            Emp localEmp2 = (Emp) paramObject2;
            return localEmp1.salary - localEmp2.salary;
        }
    }

My main class is…

class EmpListDemo
{
      public static void main(String arg[])
      {
          List  list=new ArrayList ();
          list.add(new Emp("Ram","Trainer",34000));
          list.add(new Emp("Sachin","Programmer",24000));
          list.add(new Emp("Ram","Trainer",34000));
          list.add(new Emp("Priyanka","Manager",54000));
          list.add(1,new Emp("Ravi","Administrator",44000));
          list.add(new Emp("Anupam","Programmer",34000));
          list.add(new Emp("Priyanka","Manager",54000));
          list.add(new Emp("Sachin","Team Leader",54000));
          System.out.println("There are "+list.size()+" elements in the list.");


          //original list
          System.out.println("Content of list are : ");
          ListIterator itr1=list.listIterator();
          while(itr1.hasNext())
          {
            Emp e=(Emp)itr1.next();  
            e.display();
          }           



          System.out.println("*****************");

          System.out.println("Sort Object according to Salary");
          Collections.sort(list,new SalaryComparator());
          System.out.println("Content of list are : ");
           itr1=list.listIterator();
           while(itr1.hasNext())
           {
             Emp e=(Emp)itr1.next();  
             e.display();
           }
          }
}

but please advise how to develop an application in which the arraylist will be sorted by first name and if the name of two persons are same then the further comparison will be done on the basis of their salary..!!

  • 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-09T03:15:29+00:00Added an answer on June 9, 2026 at 3:15 am
    class SalaryComparator implements Comparator {
            public int compare(Object paramObject1, Object paramObject2) {
                Emp localEmp1 = (Emp) paramObject1;
                Emp localEmp2 = (Emp) paramObject2;
                int result = localEmp1.getName().compareTo(localEmp2.getName());
                if (result == 0) {
                    result = localEmp1.salary - localEmp2.salary;
                }
                return result;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop a simple map application, which will display a map
I am trying to develop an android application which will retrieve a voicemail message
Thanks for reading! I am currently trying to develop an application which will backup
I am trying to develop an application in android in which users will be
I am trying to develop an application which will detect the color of the
I'm trying to develop an application which uses HttpRequest for requesting a web page.
I am trying to develop a mobile application which can interact with a MOSS
I am trying to develop a Facebook application which shows me all the content
I am trying to develop a web application for which I need to capture
I am trying to develop an application for my localhost on which I 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.