简体   繁体   中英

I can't figure out comparator

I'm trying to sort an arraylist but I can't wrap my head around the comparator. I don't understand how to define sortable fields from my arraylist which is created from a text file. Furthermore I'm unsure of the comparator logic. It seems to me like create a set of comparator functions, and then later invoke them. Is this true?

So far my code looks like this:

public class coord implements Comparator<Sort> {
    private int index;
    private int index2;
    private double dista;
}

public class Sort {
List<Sort> coords = new ArrayList<Sort>();


public static void main(String[] args) throws Exception {
    ArrayList dist = new ArrayList();
    File file = new File("2.txt");
    FileWriter writer = new FileWriter("2c.txt");
    try {
        Scanner scanner = new Scanner(file).useDelimiter("\\s+");

        while (scanner.hasNextLine())
        {
            int index = scanner.nextInt();
            int index2 = scanner.nextInt();
            double dista = scanner.nextDouble();
            System.out.println(index + " " + index2 + " " + dista);
        }
    }
}
        public class EmpSort {
            static final Comparator<coord> SENIORITY_ORDER =
                                         new Comparator<coord>() {
                public int compare(coord e1, coord e2) {
                    return e2.index().compareTo(e1.index());
                }
            };
            static final Collection<coord> coords = ;

            public static void main(String[] args) {
                List<Sorted>e = new ArrayList<Sorted>(coords);
                Collections.sort(e, SENIORITY_ORDER);
                System.out.println(e);

I appreciate any help anyone can give.

Comparator logic is simple. When you sort an array of elements you have two choices - sort using the Comparable on each element (assuming there is one) or supply a Comparator. If your array contains complex elements or there are different sort criteria then the latter choice is probably what you need to use.

Each time the comparator is called you must say if element 1 is "less than" element 2 in which case return a negative number, element 1 is "greater than" element 3 in which case return a positive number. Otherwise if elements are equal return 0. You may also do reference and null comparison before comparing values so that null elements are logically "less than" non-null elements and so on.

If elements are "equal" then you may wish to sort by a secondary field and then a third field and keep going until the sort order is unambiguous.

A simple comparator for a class Complex which has fields a & b and we want to sort on a:

class Complex {
  public String a = "";
  public String b = "";
}

//...

Collections.sort(someList, new Comparator<Complex>() {
  public int compare(Complex e1, Complex e2) {
    if (e1 == e2) {
      // Refs could be null or equal
      return 0;
    }
    if (e1 == null && e2 != null) {
      return -1;
    }
    if (e2 == null && e1 != null) {
      return 1;
    }
    if (e1.a == e2.a) {
      return 0;
    }
    if (e1.a == null && e2.a != null) {
      return -1;
    }
    if (e1.a != null && e2.a == null) {
      return 1;
    }
    // Just use the Comparable on the fields
    return e1.a.compareTo(e2.a);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM