Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Long values using Collections.sort(object)

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);
    }
}
like image 284
Toran Billups Avatar asked May 30 '11 12:05

Toran Billups


People also ask

How do I use Comparator in Collections sort?

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.

How do you sort a long value in Java?

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 };

Does Collections sort use compareTo?

Collections class has a second sort() method and it takes Comparator. The sort() method invokes the compare() to sort objects.

Which method is used to compare two objects for sorting?

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.


4 Answers

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 
like image 105
Rob Hoff Avatar answered Sep 16 '22 20:09

Rob Hoff


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); } 
like image 29
ratchet freak Avatar answered Sep 17 '22 20:09

ratchet freak


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));
}
like image 34
cipher Avatar answered Sep 19 '22 20:09

cipher


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;
}
like image 20
djangofan Avatar answered Sep 17 '22 20:09

djangofan