2

I have class (JavaBean if you want to call it like that)

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 should be probably look simillar to this:

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

    }

This will be in program

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

I found some references here and here. But I don't know how to complete my code.

Community
  • 1
  • 1
user1097772
  • 3,499
  • 15
  • 59
  • 95

3 Answers3

13

Your comparator should look similar to this

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

note that static int Long.compare is since Java 7

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
4

Compare method Returns: a negative integer, zero, or a positive integer as the first argument is less than, equal to, >or greater than the second.

Logic -

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

Code -

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

 }
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
3

Try This:

@Override
     public int  compare(Tweet t1, Tweet t2) {

     return t1.getMillis().compareTo(t2.getMillis());

     }

Change you mills varaible to Long if you want to use inbuilt compareTo method of Long class. Otherwise inside compare method,compare your millis from t1 and t2 like below.

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

(Directly from original Long class)

Renjith
  • 3,274
  • 19
  • 39
  • this write error: cannot compareTo(long) on the primitive type long – user1097772 Jan 25 '13 at 10:24
  • @RohitJain I don't know how upvoted it, do you think the rest of my code is correctly used with Code used from Evgenyi Dorofeev? – user1097772 Jan 25 '13 at 10:31
  • 1
    @user1097772. Yeah everything seems to be fine. But why are you using `Collections.reverse`? You only want to sort the list right? And also remember to use generic version of `Comparator` as in the Evgenyi's answer. – Rohit Jain Jan 25 '13 at 10:34
  • @RohitJain Because the millis is time in millis from 1970 when was the post (Tweet) made, and I want get the newes first (so from highest to lovest valuest) this should make Collection.reverse or am I wrong? Using generic version - I just can copy and paste the comparator from Evgenyi's it fits to the code or should be something different? I do this type of sorting first time so I'm not sure about it. – user1097772 Jan 25 '13 at 10:40
  • @user1097772.. Then you should first sort, and then reverse. Or, you can just sort in reverse order (No need to call `Collections.reverse()` in that case). You need to reverse the logic in Comparator. Just put `t2.getMillis()` as first parameter in Evgenyi's code. – Rohit Jain Jan 25 '13 at 10:41
  • @RohitJain and how can I do it in Comparator? assume that I have class TweetComparator implements Comparator { public int compare(Tweet t1, Tweet t2) { return Long.compare(t1.getMillis() ,t2.getMillis()); } } //ofcourse with Overide, but I can't put it in comment – user1097772 Jan 25 '13 at 10:45
  • 1
    @user1097772.. Change `return Long.compare(t1.getMillis() ,t2.getMillis());` to `return Long.compare(t2.getMillis() ,t1.getMillis());` – Rohit Jain Jan 25 '13 at 10:47