Sort ArrayList using the Comparable interface

advertisements

I am working on an assignment. I have the basics down of what I need done, but for some reason, the compareTo is not sorting properly on one set of example inputs. The first set of inputs sorts properly and returns the right value, but the second set rearranges, but does not sort. Basically I have an object Cow, that has a time and a number of flowers eaten. I want to take these objects and sort them, first by time, shortest time first, and then by flowers eaten, largest amount first. So if I had 3 cows, 1 and 10, 2 and 5, 2 and 7. I would sort them, first, third and second. Hopefully that makes sense. I am not sure why the Collection.sort is not working as intended...but hopefully someone can lead me in the right direction of what I am doing wrong.

Here is my code so far:

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

public class Flowers {

    public static void main(String[] args) {
        ArrayList<Cow> list = new ArrayList<Cow>();
        Scanner input = new Scanner(System.in);
        final int numOfCows = Integer.parseInt(input.next());
        int totalFlowers = 0;

        // Fills the list with the cows attributes (time and flowers destroyed)
        for (int i = 0; i < numOfCows; i++) {
            int theTime = Integer.parseInt(input.next());
            int theFlowers = Integer.parseInt(input.next());
            final Cow theCow = new Cow(theTime, theFlowers);
            list.add(theCow);
        }

        Collections.sort(list);

        for (int i = 0; i < list.size(); i++) {
            for (int k = 0; k < list.get(i).time; k++)
                for (int j = i + 1; j < list.size(); j++) {
                    totalFlowers += (list.get(j).flowers * 2);
                }
        }

        System.out.println(totalFlowers);

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        input.close();
    }

    public static final class Cow implements Comparable<Cow> {
        public final int time;
        public final int flowers;

        public Cow(final int theTime, final int theFlowers) {
            time = theTime;
            flowers = theFlowers;
        }

        @Override
        public int compareTo(Cow other) {
            int compared = 1;
            if (this.time < other.time) {
                compared = -1;
            } else {
                if (this.flowers > other.flowers) {
                    compared = -1;
                }
            }
            return compared;
        }

        @Override
        public String toString() {
            return String.format("Time:%d Flowers: %d", time, flowers);
        }
    }
}


In the statement if (this.flowers > other.flowers) you do not consider the fact that this.time could still be greater than other.time.

You do also not consider the case when time and flowers are equal to those of the other Cow.

I'd prefer doing something like this:

public int compareTo(Cow other) {
    int compare = this.time - other.time;
    if(compare == 0) {
        compare = other.flower - this.flower;
    }
    return compare;
}