0

I am having issues finding an efficient way to sort classes by order. My following code completes the order in which i need to sort, but I believe there is another way (one I dont know).

What is an efficient way to sort classes?

public int compare(Object one, Object two)    
{  
          //S = Salaried, W = Weekly, D = Daily

          //SS == 0 -> SW == -1 -> SD == -1
          //WS == 1 -> WW == 0 -> WD == -1 
          //DS == 1 -> DW == 1 -> DD == 0

          Employee a = (Employee)one;
          Employee b = (Employee)two;

          SalariedEmployee s = new SalariedEmployee(0.0); 
          WeeklyEmployee w = new WeeklyEmployee (0.0);
          DailyEmployee d = new DailyEmployee();


          if(one.getClass() == s.getClass() && two.getClass() == s.getClass())
              return Double.compare(b.grossPay(), a.grossPay());

          if(one.getClass() == s.getClass() && two.getClass() == w.getClass())
              return -1;

          if(one.getClass() == s.getClass() && two.getClass() == d.getClass())
              return -1;

          if(one.getClass() == w.getClass() && two.getClass() == s.getClass())
              return 1;

          if(one.getClass() == w.getClass() && two.getClass() == w.getClass())
              return Double.compare(b.grossPay(), a.grossPay());

          if(one.getClass() == w.getClass() && two.getClass() == d.getClass())
              return -1;

          if(one.getClass() == d.getClass() && two.getClass() == s.getClass())
              return 1;

          if(one.getClass() == d.getClass() && two.getClass() == w.getClass())
              return 1;

          if(one.getClass() == d.getClass() && two.getClass() == d.getClass())
              return Double.compare(b.grossPay(), a.grossPay());

          return 0;

      }
4

4 回答 4

3

compareTo()在您的类中实现 Comparable<> 接口并在 Employee 类中覆盖方法。该方法将 Object 类作为传递值。例如,

public class Employee implements Comparable<Employee> {
    //omitted

    public int compareTo(Employee other) {
        return grossPay.compareTo(other.grossPay);
    }
}

查看以下链接以了解更多信息 http://download.oracle.com/javase/tutorial/collections/interfaces/order.html

于 2013-06-24T00:36:54.933 回答
0

您将需要首先实现可比较的接口。这使您可以定义一个 compareTo 方法,该方法可用于根据您认为可用于比较类的特定值对类进行排序。

定义 compareTo 方法对于没有预定义的自我比较方式的对象很有用。

于 2013-06-24T07:57:14.700 回答
0

我在寻找一种按顺序对类进行排序的有效方法时遇到问题

取决于您所说的“高效”是什么意思。从 CPU 的角度来看,将所有代码放在一个方法中将是最有效的(如果处理得当的话),但从灵活性的角度来看,它并不是很有效。

对于一种不会很快但会更灵活的方法,请查看Group ComparatorBean Comparator

GroupComparator 允许您将多个比较器组合成一种。BeanComparator 是一个通用比较器,它允许您对给定类中的任何字段进行排序。因此,要使用 GroupComparator,基本代码将是:

EmployeeComparator employee = new EmployeeComparator();
BeanComparator grossPay = new BeanComparator(Employee.class, "grossPay");
GroupComparator gc = new GroupComparator(employee, grossPay);
Collections.sort(list, gc);

因此,您需要编写一个比较器,按薪水、每周和每天对员工进行排序。EmployeeComparator 的基本代码可能类似于:

if (one.getClass()equals(two.getClass())
    return 0;

if (one instanceOf SalariedEmployee)
    return 1;

if (two instanceOf SalariedEmployee)
   return -1;

if (one instanceOf WeeklyEmployee)
    return 1;
else
    return -1;

需要做更多的设置工作,但是一旦有了 EmployeeComparator,您就可以使用 Bean 和 Group Comparator 对多个不同的属性进行排序。

于 2013-06-24T00:50:21.873 回答
0

这是我的解决方案。

public int compare(Employee left, Employee right) {
    int typeOrderLeft = getTypeOrder(left);
    int typeOrderRight = getTypeOrder(right);

    if (typeOrderLeft == typeOrderRight) {
        return Double.compare(left.grossPay(), right.grossPay());
    } else {
        return typeOrderLeft - typeOrderRight;
    }
}

private int getTypeOrder(Employee employee) {
    if (employee instanceof DailyEmployee) {
        return 1;
    } else if (employee instanceof WeeklyEmployee) {
        return 2;
    } else if (employee instanceof SalaryEmployee) {
        return 3;
    }

    return 0;
}
于 2013-06-24T01:25:50.850 回答