Java 使用可比较接口对数组进行排序

Java 使用可比较接口对数组进行排序,java,interface,comparable,Java,Interface,Comparable,我正试图按克拉大小对钻石进行排序。如果大小相等,那么我想根据颜色或清晰度对它们进行排名,以给出最佳排名的为准。我不能让它正常工作。它运行,但是它将只根据克拉的大小排列它们 Crawford_Diamond[] stones = new Crawford_Diamond[16]; stones[0] = new Crawford_Diamond( "A1023", 1.0, "VS1", 'F', "brilliant"); stones[1] = new Crawford_Diamond(

我正试图按克拉大小对钻石进行排序。如果大小相等,那么我想根据颜色或清晰度对它们进行排名,以给出最佳排名的为准。我不能让它正常工作。它运行,但是它将只根据克拉的大小排列它们

Crawford_Diamond[] stones = new Crawford_Diamond[16];

stones[0] = new Crawford_Diamond( "A1023", 1.0, "VS1",  'F', "brilliant");
stones[1] = new Crawford_Diamond( "A5911", 1.1, "VVS2", 'G', "rose");
stones[2] = new Crawford_Diamond( "C5427", 1.0, "VS1",  'D', "princess");
stones[3] = new Crawford_Diamond( "D8307", 1.6, "SI1",  'H', "brilliant");
stones[4] = new Crawford_Diamond( "B4825", 0.3, "I1",   'D', "rose");
stones[5] = new Crawford_Diamond( "A1844", 2.1, "VS2",  'D', "lozenge");
stones[6] = new Crawford_Diamond( "A3747", 3.1, "SI2",  'W', "baguette");
stones[7] = new Crawford_Diamond( "E6393", 2.3, "VS2",  'I', "brilliant");
stones[8] = new Crawford_Diamond( "C5619", 2.8, "VVS1", 'E', "pear");
stones[9] = new Crawford_Diamond( "E8348", 1.4, "VS2",  'G', "brilliant");
stones[10] = new Crawford_Diamond( "D2381", 1.7, "I3",   'G', "brilliant");
stones[11] = new Crawford_Diamond( "C9253", 1.3, "VS2",  'H', "baguette");
stones[12] = new Crawford_Diamond( "G3459", 2.1, "VS2",  'H', "rose");
stones[13] = new Crawford_Diamond( "B3598", 2.4, "VVS2", 'D', "pear");
stones[14] = new Crawford_Diamond( "D9836", 2.8, "IF",   'E', "princess");
stones[15] = new Crawford_Diamond( "E1046", 2.2, "FL",   'E', "rose");

Arrays.sort(stones);

for ( int j=0; j<stones.length; j++)
    System.out.println( stones[j].toString());
public class Crawford_Diamond implements Comparable<Crawford_Diamond>
{

private String stockNumber; //diamond stock number
private double carot; //carrot size
private String clarity;  
private char color; //color of the diamond. D=Best Z=Worst
private String cut; //cut of the diamond 
private int diamondColor;
private int diamondClarity;

public Crawford_Diamond(String sN, double car, String clar, char col, String cutType)
{
    stockNumber = sN; 
    carot = car; 
    clarity = clar; 
    color = col; 
    cut = cutType;

}
//gets the stock number of the diamond
public String getStock(){return stockNumber; }

//gets the carrot size of the diamond
public double getCarot(){return carot;}

//gets the clarity of the diamond
public String getClarity(){return clarity;}

//gets the color of the diamond
public char getColor()
{

    return color;
}

//gets the cut of the diamond 
public String getCut() {return cut;}
public int compareClarity(String getClarity)
{
    int diamondClarity=0; 
    if (getClarity.equals("Fl"))
        diamondClarity = 10; 
    else if (getClarity.equals("IF"))
        diamondClarity = 9; 
    else if (getClarity.equals("VVS1"))
        diamondClarity = 8; 
    else if (getClarity.equals("VVS2"))
        diamondClarity = 7; 
    else if (getClarity.equals("VS1"))
        diamondClarity = 6; 
    else if (getClarity.equals("VS2"))
        diamondClarity = 5; 
    else if (getClarity.equals("SI1"))
        diamondClarity = 4; 
    else if (getClarity.equals("SI2"))
        diamondClarity = 3; 
    else if (getClarity.equals("I1"))
        diamondClarity = 2; 
    else if (getClarity.equals("I2"))
        diamondClarity = 1; 
    else if (getClarity.equals("I3"))
        diamondClarity = 0; 
    return diamondClarity;
}

public int getRankD1( )
{
int rankD1=0;
if (this.diamondColor > this.diamondClarity)
    rankD1 = diamondColor;  
else if (this.diamondColor < this.diamondClarity)
    rankD1 = diamondClarity;
return rankD1; 
}
public int getRankD2()
{
int rankD2=0;
if (this.diamondColor > this.diamondClarity)
rankD2 = diamondColor; 
else if (this.diamondColor < this.diamondClarity)
rankD2 = diamondClarity;
return rankD2;
}

public int compareColor(char getColor)
{
    int diamondColor=0; 
    if (getColor=='D' || getColor=='E')
        diamondColor = 10; 
    else if (getColor=='F' || getColor=='G')
        diamondColor = 9; 
    else if (getColor=='H' || getColor=='I')
        diamondColor = 8; 
    else if (getColor=='J' || getColor=='K')
        diamondColor = 7; 
    else if (getColor=='L' || getColor=='M')
        diamondColor = 6; 
    else if (getColor=='N' || getColor=='O')
        diamondColor = 5; 
    else if (getColor=='P' || getColor=='Q')
        diamondColor = 4; 
    else if (getColor=='R' || getColor=='S')
        diamondColor = 3; 
    else if (getColor=='T' || getColor=='U')
        diamondColor = 2; 
    else if (getColor=='V' || getColor=='W')
        diamondColor = 1; 
    else if (getColor=='X' || getColor=='Y')
        diamondColor = 0; 
    return diamondColor;
}

public int compareTo(Crawford_Diamond other)
{

    if (this.carot > other.getCarot())
    {
        return -1;
    }
    else if (this.carot < other.getCarot())
    {
        return 1;

    }
    else if(this.getRankD1() > other.getRankD2())
    {
        return -1;
    }
    else if(this.getRankD1() < other.getRankD2())
    {
        return 1;
    }
    else 
        return 0;}



public String toString()
{
    return "{stockNumber :: " +getStock() + " carot :: " +getCarot() + " clarity :: " +getClarity()+ " color :: " +getColor() + " cut :: " +getCut()+"}";
}
}   
Crawford_Diamond[]stones=新Crawford_Diamond[16];
石头[0]=新克劳福德钻石(“A1023”,1.0,“VS1”,“F”,“辉煌”);
stones[1]=新克劳福德钻石(“A5911”,1.1,“VVS2”,“G”,“玫瑰”);
石头[2]=新克劳福德钻石(“C5427”,1.0,“VS1”,“D”,“公主”);
石头[3]=新克劳福德钻石(“D8307”,1.6,“SI1”,“H”,“辉煌”);
石头[4]=新克劳福德钻石(“B4825”,0.3,“I1”,“D”,“玫瑰”);
石头[5]=新克劳福德钻石(“A1844”,2.1,“VS2”,“D”,“菱形”);
石头[6]=新克劳福德钻石(“A3747”,3.1,“SI2”,“W”,“法式面包”);
石头[7]=新克劳福德钻石(“E6393”,2.3,“VS2”,“I”,“辉煌”);
石头[8]=新克劳福德钻石(“C5619”,2.8,“VVS1”,“E”,“梨”);
石头[9]=新克劳福德钻石(“E8348”,1.4,“VS2”,“G”,“辉煌”);
石头[10]=新克劳福德钻石(“D2381”,1.7,“I3”,“G”,“辉煌”);
石头[11]=新克劳福德钻石(“C9253”,1.3,“VS2”,“H”,“法式面包”);
石头[12]=新克劳福德钻石(“G3459”,2.1,“VS2”,“H”,“玫瑰”);
石头[13]=新克劳福德钻石(“B3598”,2.4,“VVS2”,“D”,“梨”);
石头[14]=新克劳福德钻石(“D9836”,2.8,“如果”,“E”,“公主”);
stones[15]=新克劳福德钻石(“E1046”,2.2,“FL”,“E”,“rose”);
数组。排序(石头);
对于(int j=0;j这个直径)
rankD1=钻石色;
else if(this.diamondColorthis.diamondclasticity)
rankD2=钻石色;
else if(this.diamondColorother.getCarot())
{
返回-1;
}
else if(this.carotother.getRankD2())
{
返回-1;
}
else if(this.getRankD1()
尝试确定,因为它们是双精度而不是整数

public int compareTo(Crawford_Diamond other)
{  
    int value = 0;
    if (this.carot > other.getCarot())
    {
        value = -1;
    }
    else if (this.carot < other.getCarot())
    {
        value = 1;
    }       
    if (value == 0){
       value = getRankD1() - other.getRankD1();           
    }
    return value;
}  
公共内部比较(克劳福德钻石其他)
{  
int值=0;
if(this.carot>other.getCarot())
{
值=-1;
}
else if(this.carot
在这种情况下,我发现给值一个绝对分数更容易

首先,我将更改函数以相反的顺序返回值(其中Color
D
的分数为0,Color
X
的分数为10。同样,最好的清晰度('fl'已经是高分了……)

使用最佳属性排名最高的分数。你的分数范围为0到10

因此,我在Crawford_Diamond上有一个名为
getScore()
的方法,它可以:

public int getScore() {
    int score = (int)(carat * 100000); // carat of 2.2 will become 220000
    int colorscore = getColorScore(color);
    int clarityscore = getClarityScore(clarity);
    if (color > clarity) {
        // color of D is best, give it score 1000 and add the clarity score
        score = score + (100 * color) + clarity;
    } else {
        score = score + (100 * clarity) + color;
    }
    return score;
}
然后,您的compareTo方法简单地变成:

public int compareTo(Crawford_Diamond other) {
    return other.getScore() - getScore();
}

(注意,它是克拉,而不是卡洛特(或插入符号))

您几乎完全正确。如果您只需在构造函数中添加两行代码,您的代码将完全满足您的要求

public Crawford_Diamond(String sN, double car, String clar, char col, String cutType)
{
    stockNumber = sN; 
    carot = car; 
    clarity = clar; 
    color = col; 
    cut = cutType;

    diamondColor = compareColor(color);
    diamondClarity = compareClarity(clarity);
}
您还可以做其他事情来清理代码。您可以将rename
compareClarity
更改为
getClarityValue
,因为您只是将字符串转换为整数。颜色也是如此


您也不需要两个版本的ranking方法。由于该方法始终在给定的diamond对象上调用,因此一个
getRank()
方法可以很好地替代
getRankD1()
getRankD2()

对于
清晰性
字段,您确实应该使用
枚举
而不是字符串;它更简单且类型安全,您可以消除整个
开关
。如果需要以多种方式对对象排序,请使用自定义比较器。您的代码不会编译。它会返回
双精度
,而不是
int
非常感谢你。它现在工作得很好,看起来更干净了。