Reputation: 1935
My goal is to sort an ArrayList to ascending size. I have this code:
Collections.sort(files, SizeFileComparator());
Or should it be:
Collections.sort(files, new SizeFileComparator());
They both do not work, and I get an error that SizeFileComparator() is not an existing class. I am importing java.io.*; and this is the link to the api of SizeFileComparator: http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/comparator/package-summary.html
I assume something is wrong in the way I implement the API.
Upvotes: 1
Views: 110
Reputation: 311393
SizeFileComparator
isn't under java.io
, it's in org.apache.commons.io.comparator
, and that's where you need to import it from. Either explicitly:
import org.apache.commons.io.comparator.SizeFileComparator;
Or via a wildcard import:
import org.apache.commons.io.comparator.*;
Regardless, you'd also need to make sure that the JAR containing it (some revision of commons-io.jar) is present in your classpath.
Once you properly import it, you need an instance in order to use it. While you could instantiate it directly, it's better to use one of the predefined constants:
Collections.sort(files, SizeFileComparator.SIZE_COMPARATOR);
Upvotes: 1