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.


Spotify Play Button sizing in hidden elements

We have some embedded Spotify play buttons in a paged quiz style wizard/carousel (using jQuery Tools -yuk- to provide the paging functionality); the issue I'm having is that because each question is on a div that is initially hidden, the content of each Spotify iframe is unable to work out which player to render (small vs large).

It would be possible to force a refresh of these iframes when the user scrolls to each panel, but this feels like a hack and a bunch of extra HTTP requests.

Has anyone had any experience with this? Any workarounds that don't require me to multiply requests to Spotify?

Thanks in advance

S

4

3 回答 3

13

您的比较器应该与此类似

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 开始

于 2013-01-25T10:22:11.813 回答
4

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

逻辑 -

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

 }
于 2013-01-25T10:18:32.927 回答
3

尝试这个:

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

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

     }

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

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

(直接来自原龙班)

于 2013-01-25T10:18:27.340 回答