I'm trying to sort a simple list of objects by a long - the below isn't working because one of the long strings is pushed to the top simply because it starts with a lower number. So I'm looking for a way to sort these by the actual long values directly
The current obj implementation looks something like the below. In the class I'm using this I call Collections.sort(trees);
public class Tree implements Comparable<Tree> {
public String dist; //value is actually Long
public int compareTo(Tree o) {
return this.dist.compareTo(o.dist);
}
}
Using a comparator, we can sort the elements based on data members. For instance, it may be on roll no, name, age, or anything else. Method of Collections class for sorting List elements is used to sort the elements of List by the given comparator.
To sort long array in Java, use the Arrays. sort() method. Let's say the following is our long array. long[] arr = new long[] { 987, 76, 5646, 96,8768, 8767 };
Collections class has a second sort() method and it takes Comparator. The sort() method invokes the compare() to sort objects.
The compareTo method defines the natural order; the default way for ordering objects of a class. It should return a negative integer(usually -1), if the current triggering object is less than the passed one, and positive integer (usually +1) if greater than, and 0 if equal.
Long.compare( x , y )
If you have an object that you want to sort on a long
value, and it implements Comparable
, in Java 7+ you can use Long.compare(long x, long y)
(which returns an int
)
E.g.
public class MyObject implements Comparable<MyObject> { public long id; @Override public int compareTo(MyObject obj) { return Long.compare(this.id, obj.id); } }
Call Collections.sort(my_objects)
where my_objects is something like
List<MyObject> my_objects = new ArrayList<MyObject>(); // + some code to populate your list
why not actually store a long in there:
public class Tree implements Comparable<Tree> { public long dist; //value is actually Long public int compareTo(Tree o) { return this.dist<o.dist?-1: this.dist>o.dist?1:0; } }
that or first compare the length of the strings and then compare them
public String dist; //value is actually Long public int compareTo(Tree o) { if(this.dist.length()!=o.dist.length()) return this.dist.length()<o.dist.length()?-1:1;//assume the shorter string is a smaller value else return this.dist.compareTo(o.dist); }
well if the dist variable is actually long then you might try using
public int compareTo(Tree o) {
return Long.valueOf(this.dist).compareTo(Long.valueOf(o.dist));
}
Just an example I made for sorting Files by date using a Long comparator:
public File[] getAllFoldersByDescendingDate(File folder) {
if (!folder.isDirectory()) {
return null;
}
allFiles = folder.listFiles();
Arrays.sort(allFiles, new Comparator<File>()
{
public int compare(final File o1, final File o2)
{
return Long.compare(o2.lastModified(), o1.lastModified());
}
});
return allFiles;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With