使用Collections.sort(object)比较Long值

26

我正在尝试按照long类型对一个简单对象列表进行排序,但以下代码不起作用,因为其中一个较小的数字会被推到列表顶部。因此,我要寻找一种直接按照实际long数值排序的方法。

当前对象的实现大致如下。在使用此类时,我调用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);
    }
}

为什么不在compareTo(Tree o)方法中比较“dist”的长整型值呢? - Jacob
1
字符串按字母顺序进行比较(虽然不完全是这样,但这是一种很好的思考方式)。因此,12 > 111。如果您需要比较数字,请使用数字,即添加一个更长的字段。或者在最坏的情况下,比较Long.parseLong(dist)的结果。 - bestsss
结果发现我一直在比较一个双精度数 - 对不起,伙计们(最终采用Double.compare作为我的解决方案)。尽管如此,我认为对于Long问题给出了一个很好的答案 - 如果没有其他问题,请标记它以进行删除。 - Toran Billups
使用 'Long.compare(long x, long y)' 方法,参见下面的答案。 - Rob Hoff
6个回答

50

Long.compare( x , y )

如果你有一个对象,想要根据其long值进行排序,并且实现了Comparable接口,在Java 7或以上版本中,可以使用Long.compare(long x, long y)(返回一个int)。

例如:

public class MyObject implements Comparable<MyObject>
{
  public long id;

  @Override
  public int compareTo(MyObject obj) {
    return Long.compare(this.id, obj.id);
  }
}

调用Collections.sort(my_objects),其中my_objects是类似于以下内容的东西

  List<MyObject> my_objects = new ArrayList<MyObject>();
  // + some code to populate your list

10
仅适用于 API 等级 19 及以上版本。 - Analizer
有没有针对 KitKat 之前版本的任何想法? - Alireza Noorali

21
为什么不实际将一个长整型存储在那里呢?
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;
    }
}

首先比较字符串的长度,然后再进行比较。

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

31
更新:自Java 7以来,您可以使用Long.compare(x, y)代替编写此类代码(这不是一项革命性的改变,但更加简便)。 - Christophe Roussy
7
在安卓系统中,@ChristopheRoussy的解决方案仅适用于API 19及以上版本。 - Imdad
1
更新:您甚至可以使用Comparator.comparingLong(x::y) - Big Dude

13

好吧,如果dist变量实际上是一个长整型,那么你可以尝试使用

public int compareTo(Tree o) {
    return Long.valueOf(this.dist).compareTo(Long.valueOf(o.dist));
}

无法编译(Long.parseLong返回原始长整型) - bestsss

5

这是我用 Long 比较器编写的一个根据日期排序文件的示例:

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
这取决于您想如何做?您是否想保留Comparable的当前实现?如果是,请使用带有Comparator的sort方法,并实现一个自定义比较器,该比较器使用字符串的实际“long”值(Long.parseLong(dist))。如果不是,则只需修改当前的compareTo并使用“dist”的Long值。

顺便说一下,我会重新审视逻辑并问自己为什么“dist”是String类型,而实际上它是Long类型?


1

为什么不呢

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

    public int compareTo(Tree o) {
        return this.dist.compareTo(o.dist);
    }
}

如果性能是个问题的话,最好使用原始方法。否则,似乎有人指出:“皇帝是赤裸的!” :) - Positive Navid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接