We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › sort array of objects by String inside variable
Pages: 1 2 
sort array of objects by String inside variable (Read 2470 times)
Re: sort array of objects by String inside variabl
Reply #15 - Feb 25th, 2009, 7:46am
 
Hello,

I have been trying to get my code to work using the information in this thread, but it seems to be problems no matter what i try. Here is my code:

Collections.sort(packets, new Comparator(){

  public int compare(Object o1, Object o2) {
    long p1 = ((TCPPacket)o1).getSequenceNumber();
    long p2 = ((TCPPacket)o2).getSequenceNumber();

    return p1.compareToIgnoreCase(p2);
 }
}

This gives me the following error:

"Cannot invoke compareToIgnoreCase(long) on the primitive type long".

So I tried to follow WombatNations advice and did the following:

Collections.sort(packets, new Comparator(){

  public int compare(Object o1, Object o2) {
    Long p1 = new Long(((TCPPacket)o1).getSequenceNumber());
    Long p2 = new Long(((TCPPacket)o2).getSequenceNumber());

    return p1.compareToIgnoreCase(p2);
 }
}


And now i'm getting a different error:

"The method compareToIgnoreCase(Long) is undefined for the type Long".

Any help to solve this would be greatly appreciated.

Thank you in advance for your time.

M
Re: sort array of objects by String inside variabl
Reply #16 - Feb 25th, 2009, 1:10pm
 
Why are you ignoring case on numbers? They have no case...

I think you need to write:

 public int compare(Object o1, Object o2) {
    long p1 = ((TCPPacket)o1).getSequenceNumber();
    long p2 = ((TCPPacket)o2).getSequenceNumber();

    return p1 == p2 ? 0 : (p1 > p2 ? 1 : -1);
 }

Actually, checking back thread, WombatNations already shows that for floats.
Pages: 1 2