Comparing Long values using Collections.sort(objec

2019-02-16 08:04发布

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

6条回答
Emotional °昔
2楼-- · 2019-02-16 08:48

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;
}
查看更多
贼婆χ
3楼-- · 2019-02-16 08:50

Why not

public class Tree implements Comparable<Tree> {
    public Long dist;

    public int compareTo(Tree o) {
        return this.dist.compareTo(o.dist);
    }
}
查看更多
地球回转人心会变
4楼-- · 2019-02-16 08:54

It depends on how you want to do things? Do you want to keep the current implementation of Comparable? If yes, use the sort method which takes a Comparator and implement a custom comparator which uses the actual "long" values of the string (Long.parseLong(dist)). If no, then just modify the current compareTo and use the Long values of the "dist".

BTW, I'd revisit the logic and ask myself why "dist" is of type String when it is actually a Long?

查看更多
爷、活的狠高调
5楼-- · 2019-02-16 08:55

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
查看更多
别忘想泡老子
6楼-- · 2019-02-16 09:00

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);
}
查看更多
Deceive 欺骗
7楼-- · 2019-02-16 09:01

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));
}
查看更多
登录 后发表回答