Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

java - As overload the method compare the Comparator interface?

I am developing a small application , I have an "Employee" class with methods and corresponding attributes (name , salary , date .. ) and another class that is a list of employees , which I use an arraylist to store employees and different methods , such as adding employee , delete employee .

Now I 'm trying to sort employees by alphabetical order and date of hire (lowest to highest).

But I have a little problem, I can not use the method compare more than 2 times, I have been researching and not whether it could overload the method compare to sort employees by name and date . thanks.

    public class Employee
      {

        private String name;
        private GregorianCalendar dateHire;

     public GregorianCalendar getDateHire() {  return  dateHire;  }

  public void setDateHire(GregorianCalendar  dateHire) { this.dateHire =  dateHire; }


public getName() { return name;}

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



     }

//------------------------------------------------------

  public class listEmployee implements Comparator <Employee>
   {

           ArrayList<Employee> Employees = new ArrayList<>();

              @Override
            public int compare(Employee e1, Employee e2) {
                if (e1.getName() != e2.getName()) {
                    return 1;
                } else {
                    return -1;
                }
            }  


// now , when I call this function in the parent class , if you sort the names , now when I try 
//to sort dates , not let me use the method compare more than twice

     public void sortAlphabetical() {

            Collections.sort(trabajadores,Employee.StuNameComparator);
        }

        //I was looking online and found these options, but not as a call in the main class

              public static Comparator <Employee > sortDate = new Comparator<Employee >()
            {

                @Override
                public int compare (Employee s1, Employee s2)
                {
                   GregorianCalendar empTemporal = s1.getDateHire();
                     GregorianCalendar emTemporal2 = s2.getDateHire();

                     return  empTemporal.compareTo(emTemporal2);

                }


            };





          public static Comparator <Employee> StuNameComparator = new Comparator<Employee >() {

                @Override
            public int compare(Employee  s1, Employee  s2) {
               String EmployeName1 = s1.getName().toUpperCase();
               String EmployeName2 = s2.getName().toUpperCase();

               //ascending order
               return EmployeName1.compareTo( EmployeName2);

               //descending order
               //return StudentName2.compareTo(StudentName1);
            }};
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't overload the compare method of the Comparator<Employee> interface, since there is just one signature to that method - int compare(Employee s1, Employee s2).

However, you can have multiple classes that implement that interface, and you can choose the relevant Comparator<Employee> implementation whenever you wish to sort your Employee list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...