Yes, a Java Comparator should be able to be used directly inside of a by() modulator. I've tested something similar to your code and it works.
GraphTraversalSource g = TinkerGraph.open().traversal();
g.addV().property("date", "2021-02-01").iterate();
g.addV().property("date", "2023-07-22").iterate();
g.addV().property("date", "2022-05-12").iterate();
Comparator<Object> vertexComparator = new Comparator<Object>() {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
@Override
public int compare(Object o1, Object o2) {
try {
Date leftDate = fmt.parse(o1.toString());
Date rightDate = fmt.parse(o2.toString());
return leftDate.compareTo(rightDate);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
};
System.out.println(g.V().values("date").order().by(vertexComparator).toList());
[2021-02-01, 2022-05-12, 2023-07-22]
A couple quick notes:
1. I was getting parsing exceptions using the format provided in your examples so I had to change it.
2. I chose to call V().values() instead of calling values() on the returned Vertex as it is recommended to use the process API (although for this example they are mostly equivalent so either would work).