I will try again.
This code creates and prints the array but gives me an error at Collections.sort.This compiles and runs well: public class IntegerTest implements Comparator<Integer>{ @Override public int compare(Integer o1, Integer o2){ return (o1<o2 ? -1 : (o1==o2 ? 0 : 1)); } /** * @param args the command line arguments */ public static void main(String[] args) { List<Integer> arrayList = new ArrayList<>(); arrayList.add(12); arrayList.add(5); arrayList.add(7); arrayList.add(4); arrayList.add(5); arrayList.add(16); arrayList.add(13); arrayList.add(2); System.out.println(arrayList); Collections.sort (arrayList, new IntegerTest()); for (Integer integer : arrayList){ System.out.println (integer); } } }
What is wrong with the call?
public class ListSorter implements Comparator<Integer>{ @Override public int compare(Integer o1, Integer o2){ return (o1>o2 ? -1 : (o1==o2 ? 0 : 1)); } } public static void main(String[] args)throws IOException { java.io.File intDat = new java.io.File("intDat"); try (java.io.PrintWriter create = new java.io.PrintWriter (intDat)) { create.print("23 45 67 12 9 44 14 21 78 95 6 89 7 42 28 34"); } Collection<Integer> intList; try (Scanner read = new Scanner (intDat)) { intList = new ArrayList<>(); //List<Integer> arrayList = new ArrayList<>(); while (read.hasNext()){ intList.add(read .nextInt()); System.out.println(intList); } } //System.out.println(arrayList); Collections.sort (intList, new ListSorter()); for (Integer integer : intList){ System.out.println (integer); } } }