2 ms·
I haven't tried this but according to the comments on the post you can do something like this: Comparator string_lessp = (Comparator) #(String a, String b)
by tlrobinson 16y ago
I haven't tried this but according to the comments on the post you can do something like this:
Comparator string_lessp = (Comparator) #(String a, String b) {
return a.compareToIgnoreCase(b);
}
sort(list, string_lessp);
- jpr 16y agoYeah, but that just seems wrong. It's like "I have this function, but I can't actually refer to it, so I'll have to wrap it in this thingamajig just so that I can pass it around".
- tlrobinson 16y agoHuh? You can, but the sort method already takes a Comparator class. If it took a function you could pass around the function instead. Anyway, the point was only that "(sort list #'string-lessp)" is nothing special.
- jpr 16y agoThe definition of Comparator-interface includes only two methods, .compare and .equals. The documentations says that it is always safe to not override .equals, so we're left with just .compare. This effectively means that each comparator is effectively a function of two arguments that returns a boolean. Had functions been in Java in the first place, the whole Comparator interface would have been superfluous.