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これはJava7以降であることに注意してください

于 2013-01-25T10:22:11.813 に答える
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;
     }

 }
于 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 に変更します。それ以外の場合は、compare メソッド内で、以下のように t1 と t2 のミリ秒を比較します。

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

(オリジナルのロングクラスから直接)

于 2013-01-25T10:18:27.340 に答える