Using compareTo and Collections.sort

前端 未结 4 1301
太阳男子
太阳男子 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:51

    It is because at first comparision condition you are comparing on the basis of state. If the state of current object is not small, then only comparision based on sales will take place. According to your code, in state you want the state of current object to be less than the comparing state, however in sales comparision you want the sales of current object to be greater than the comparing object. This is why you are getting different results. States are being compared in ascending order and sales in descending order. It is all dependent on what you return from compareTo function.

    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;
    }
    

    Hope this code will help you. You can find good explanation over here

    0 讨论(0)
  • 2021-01-06 18:51

    Comparable will give only one way of comparision. This can be done using Comparator interface.

    Collections.sort(list, new Comparator<Franchise>() {
    @Override
                public int compare(Franchise obj1, Franchise obj2) {
                    if(obj1.getState().compareTo(obj2.getState()) == 0)
                    {
                        Double a1 =obj1.getSales();
                        Double a2 = obj2.getSales();
                        return a2.compareTo(a1);
                    }
    
                        return o1.getName().compareTo(o2.getName());
            } 
    }
    
    0 讨论(0)
  • 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;         
    }
    
    0 讨论(0)
  • 2021-01-06 18:59

    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.

    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.

    0 讨论(0)
提交回复
热议问题