Using compareTo and Collections.sort

前端 未结 4 1302
太阳男子
太阳男子 2021-01-06 18:27

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)<

4条回答
  •  一整个雨季
    2021-01-06 18:54

    You have to modify your compareTo method. Cause you are returning after comparing the state. So you have to compare state but sales too.

    For example:

    public int compareTo(Franchise that) {
        int stateComparition = this.getState().compareTo(that.getState()); 
        Double sales = Double.valueOf(this.getSales());    
        Double thatSales = Double.valueOf(that.getSales());
        int salesComparition = sales.compareTo(thatSales);    
    
        if(stateComparition == 0){
            if(salesComparition > 0)
                 return -1;
            else if(salesComparition < 0)
                 return 1;
            else
                 return 0;
        }
           return stateComparition;         
    }
    

提交回复
热议问题