【问题标题】:How to sort arrayList<Object> by object attribute long in Java如何在Java中按对象属性长排序arrayList<Object>
【发布时间】:2013-01-25 10:15:24
【问题描述】:

我有课(JavaBean,如果你想这样称呼它)

class Tweet{

private millis; //number of millis since 1970
//other attributes and getters and setters, but i want to sort onlny by millis

public long getMillis() {
   return millis;
}

}

Comparator 应该看起来与此类似:

class TweetComparator implements Comparator {
     @Override
     public int  compare(Tweet t1, Tweet t2) {
     //something
     //this doesn't work
     //return t2.getMillis().compareTo(t1.getMillis());
     return ??;//what should be here?
     }

    }

这将在程序中

List<Tweet> tweets = new ArrayList<Tweet>();
tweets.add(...); //just fill the list
//i need newest (with hightest millis value first) so I probably need to call reverse order
Collection.reverse(tweets)
Collection.sort(tweets, new TweetComparator());

我找到了一些参考资料herehere。但我不知道如何完成我的代码。

【问题讨论】:

  • 当您尝试注释掉的代码时发生了什么?
  • @RohitJain 写入错误:无法在原始类型 long 上进行 compareTo(long)

标签: java sorting collections compare long-integer


【解决方案1】:

您的比较器应该与此类似

class TweetComparator implements Comparator<Tweet> {
    @Override
    public int compare(Tweet t1, Tweet t2) {
        return Long.compare(t1.getMillis(), t2.getMillis()); 
    }
}

请注意,static int Long.compare 是从 Java 7 开始的

【讨论】:

  • Collection.reverse(tweets) 是不必要的
  • 你确定吗?即使我需要它从最新到最旧(我的意思是从最高毫秒到最低毫秒值)打印它?
  • @user1097772.. 然后将t2.getMillis() 作为compare 中的第一个参数移动。而t1.getMillis() 作为第二个参数。它会自动反向排序。
  • 那么你应该排序,打印(升序),反向,打印(降序)。
  • 如果您在这里是为了实现 android,那么这仅适用于 api 19。
【解决方案2】:

比较方法返回: 负整数、零或正整数作为第一个参数小于、等于、> 或大于第二个参数。

逻辑-

if t1.millis > t2.millis 
   return -1;
else if t1.millis < t2.millis
   return +1;

代码-

class TweetComparator implements Comparator<Tweet> {
     @Override
     public int  compare(Tweet t1, Tweet t2) {
        if(s1.i>s2.i)
            return -1;
        else if(s1.i<s2.i)
            return +1;
        return 0;
     }

 }

【讨论】:

    【解决方案3】:

    试试这个:

    @Override
         public int  compare(Tweet t1, Tweet t2) {
    
         return t1.getMillis().compareTo(t2.getMillis());
    
         }
    

    如果您想使用 Long 类的内置 compareTo 方法,请将您的 mills 变量更改为 Long。 否则在 compare 方法中,比较你的 t1 和 t2 中的毫秒,如下所示。

    long t1Val = t1.getMillis();
    long t2Val = t2.getMillis();
    return (t1Val<t2Val? -1 : (t1Val ==t2Val? 0 : 1));
    

    (直接来自原始 Long 类)

    【讨论】:

    • 此写入错误:无法在原始类型 long 上进行比较(long)
    • @RohitJain 我不知道它的投票率如何,您认为我的其余代码与 Evgenyi Dorofeev 使用的代码正确使用了吗?
    • @user1097772。是的,一切似乎都很好。但是你为什么使用Collections.reverse?您只想对列表进行排序吗?并且还记得在 Evgenyi 的回答中使用 Comparator 的通用版本。
    • @RohitJain 因为毫秒是从 1970 年开始发布(推文)的时间,我想首先获取新闻(所以从最高到最喜欢的值)这应该使 Collection.reverse 或我错了吗?使用通用版本 - 我只能从 Evgenyi 复制并粘贴比较器,它适合代码还是应该有所不同?我第一次做这种类型的排序,所以我不确定。
    • @user1097772.. 将return Long.compare(t1.getMillis() ,t2.getMillis()); 更改为return Long.compare(t2.getMillis() ,t1.getMillis());
    猜你喜欢
    • 2013-05-21
    • 2020-01-12
    • 2011-08-12
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多