/* Demonstration of how to: - have 1 file with multiple classes - use Comparable to sort an ArrayList of objects */ import java.util.ArrayList; class FooBar implements Comparable { int foo; int bar; FooBar() { foo = 0; bar = 1; } FooBar(int x, int y) { foo = x; bar = y; } public String toString() { return "foo: " + foo + " bar: " + bar; } public int compareTo(FooBar fb) { if (this.foo < fb.foo && this.bar < fb.bar) { return -1; } else if (this.foo == fb.foo && this.bar == fb.bar) { return 0; } return 1; } } public class Main { public static void main(String[] args) throws IOException { ArrayList a = new ArrayList(); a.add(new FooBar(3,4)); a.add(new FooBar(3,4)); a.add(new FooBar(9,10)); a.add(new FooBar(1,1)); System.out.println(a); Collections.sort(a); System.out.println(a); } // main }// class