5

I have some code to sort paths by date modified. I want to also write some code to sort the paths in reverse order, and might later want to add some other sorting methods. Is there any way to do all the sorting from a single class file? Or do I have to create another class PathSortByDateReverse, PathSortByCreated, PathSortByFoo, etc. Also, how would I use the different sorting methods?

import java.nio.file.Path;
import java.util.Comparator;

public class PathSortByDate implements Comparator<Path> {

@Override
public int compare(Path first, Path second) {
    long seconddate = second.toFile().lastModified(); // get just the filename
    long firstdate = first.toFile().lastModified();

    if (firstdate == seconddate) {
        return 0;
    } else if (firstdate > seconddate) {
        return 1;
    } else {
        return -1;
    }
}
}

I then call it from the other class with:

public static ArrayList<Path> sortArrayListByDate(ArrayList<Path> pathlist) {
    Collections.sort(pathlist,new PathSortByDate());
    return pathlist;
}    
localhost
  • 1,253
  • 4
  • 18
  • 29

3 Answers3

3

Why not go for an anonymous inner classes?

public static final Comparator<Person> ID_DESC
     = new Comparator<Person>() {
      public int compare(Person p1, Person p2) {
         return -1 * p1.getId().comparedTo(p2.getId());
         // reversed order
      }
    };
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
  • You can get more info [here](http://stackoverflow.com/questions/10309929/implementing-comparator-multiple-times-in-a-single-class-file/10310033#10310033) – Shashank Kadne Apr 25 '12 at 05:51
2

I would usually do it like this. Notice, the constructor is "private" and there is a "public factory method" to get an instance. There will ever exist two PathComparator instances at any given point. This is a big deal if you are into optimizing your code and using best practices.

import java.nio.file.Path;
import java.util.Comparator;

final public class PathComparator implements Comparator<Path> {

// comparator for in order
final private static PathComparator ascendingOrderComparatorDate = new PathComparator(true);
// comparator for reverse order
final private static PathComparator descendingOrderComparatorDate = new PathComparator(false);

final private int isAscendingOrderInt;

final public PathComparator getPathComparator(boolean isAscendingOrder) {
    return isAscendingOrder ? ascendingOrderComparatorDate : descendingOrderComparatorDate;
}

private PathComparator(boolean isAscendingOrder) {
    this.isAscendingOrderInt = isAscendingOrder ? 1 : -1;
}

@Override
public int compare(Path first, Path second) {
    // for optimization (not required but highly recommended)
    if(first == second) return 0;

    long seconddate = second.toFile().lastModified(); // get just the filename
    long firstdate = first.toFile().lastModified();

    if (firstdate == seconddate) {
        return 0;
    } else if (firstdate > seconddate) {
        return isAscendingOrderInt * 1;
    } else {
        return isAscendingOrderInt * -1;
    }
}}
ZeX
  • 21
  • 1
1

you dont need to make the reverse comparator, just make it and reverse it with

Collections.reverseOrder()
Peter
  • 5,728
  • 20
  • 23
  • Thanks, I thought of that, but I probably want to add other sorting methods later, and basically want to understand better how it all works. – localhost Apr 25 '12 at 05:47