使用compareTo和Collections.sort

问题描述:

我有一个拥有所有者(特许经营者的名字的所有者),州(特许经营权所在州的2个字符的字符串)和销售额(一天的总销售额)的特许经营类

I have a franchise class with owner(owner of franchise's name), state(2-character string for the state where the franchise is located), and sales (total sales for the day)

public class Franchise implements Comparable <Franchise>  {
final String owner;
final String state;
final double sales;

protected Franchise(String owner, String state, double sales ) {
    this.owner = owner;
    this.state = state;
    this.sales = sales;
}
public String toString() {
    String str = state + ", " + sales + ", " + owner;
    return str;
}
public String getState() {
    return state;
}
public double getSales() {
    return sales;
}
public int compareTo(Franchise that) {
    double thatSales = that.getSales();
    if (this.getState().compareTo(that.getState()) < 0)  
        return -1;
    else if (this.getSales() > thatSales)
        return -1;
    else if (this.getSales() < thatSales)
            return 1;
    else
        return 0;
}
}

compareTo基于状态ASCENDING和销售DESCENDING

the compareTo is based on state ASCENDING and sales DESCENDING

import java.util.ArrayList;
import java.util.Collections;

public class FranchiseTester {

public static void main(String[] args) {
    ArrayList<Franchise> franchises = new ArrayList<Franchise>();
    Franchise a = new Franchise("Andrew Luck", "IN", 1270.5);
    Franchise b = new Franchise("Ray Rice", "MD", 1210);
    Franchise c = new Franchise("Alfred Morris", "WA", 980.5);
    Franchise d = new Franchise("Roddy White", "GA", 670);
    Franchise e = new Franchise("Greg Olsen", "SC", 740);
    Franchise f = new Franchise("T.Y. Hilton", "IN", 950);
    Franchise g = new Franchise("Julio Jones", "GA", 560);

    franchises.add(a);
    franchises.add(b);
    franchises.add(c);
    franchises.add(d);
    franchises.add(e);
    franchises.add(f);
    franchises.add(g);

    Collections.sort(franchises);

    for(int i = 0; i < franchises.size(); i++) {   
        System.out.print(franchises.get(i) + "\n");
    } 
}

}

比较这些特许经营对象没有 Collections.sort 他们比较正确,但是当我测试使用 Collections.sort 像我在这里我得到这样的输出:

when I compare these franchise objects without the Collections.sort they compare correctly, However when I test using the Collections.sort like I have here I get an output like this:


GA,670.0,Roddy White

GA,560.0,Julio Jones

IN,1270.5,Andrew Luck

IN,950.0,TY希尔顿

MD,1210.0,Ray Rice

SC,740.0,Greg Olsen

WA,980.5,Alfred Morris

GA, 670.0, Roddy White
GA, 560.0, Julio Jones
IN, 1270.5, Andrew Luck
IN, 950.0, T.Y. Hilton
MD, 1210.0, Ray Rice
SC, 740.0, Greg Olsen
WA, 980.5, Alfred Morris

状态仍在正确比较,但它不能正确比较销售(特定统计数据的销售额较低应该优先)

The state's are still being compared correctly but it's not comparing by sales properly (lower sales for particular stat should come first)

我认为.sort比较字符串默认是状态仍然正确的原因,我的问题是如何实现它来比较基于销售太?

I think that the .sort compares string by default is the reason that states are still correct, my question is how do I implement it to compare based on sales too?

在您的问题陈述中,您表示compareTo基于状态ASCENDING和销售DESCENDING。基于此,您的结果是有效的。国家按升序排列,对于每个州,销售按降序排列。在下一个语句中,您要说(特定统计数据的销售额较低应该优先)。所以基本上你有两个冲突的要求。两者不能同时完成。

In your problem statement you are saying that "compareTo is based on state ASCENDING and sales DESCENDING". Based on this your results are valid. States are in ascending order and for each state the sale is in descending order. In the very next statement you are saying (lower sales for particular stat should come first). So basically you have two conflicting requirement. Both can not be done simultaneously.

换句话说,你希望你的程序做一些其他的事情,比如两者都应该是升序,还是降序还是其他顺序。如果是,则必须相应地修改compareTo方法。

In other words do you want your program to do something else like both should be ascending or both descending or some other order. If yes then you have to modify your compareTo method accordingly.