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 › Use of Comparator
Page Index Toggle Pages: 1
Use of Comparator? (Read 908 times)
Use of Comparator?
Sep 13th, 2005, 11:01am
 
Hi,

how is it possible to use Comparator with P5? I get a compiler error with this code...I think its because it has to be JRE 5.0, can that be? Is it possible to run P5 with 5?

Code:

import java.util.Arrays;
import java.util.Comparator;

public class Test {
   public static void main( String[] args ) {
       int[][] test = new int[][]{
               {1, 4},
               {2, -8},
               {3, 5}
       };
       
       System.out.println( "unsorted:" );
       print( test );
       
       Arrays.sort( test, new Comparator<int[]>(){
           public int compare( int[] o1, int[] o2 ) {
               return o1[1] - o2[1];
           }
       });
       
      System.out.println( "sorted:");
       print( test );
   }
   
   public static void print( int[][] test ){
       for( int i = 0; i < test.length; i++ )
           System.out.println( Arrays.toString( test[i] ));
   }
}
Re: Use of Comparator?
Reply #1 - Sep 13th, 2005, 4:55pm
 
right, that's syntax that's specific to java 1.5 and won't work anywhere else. we don't support java 1.5 for processing and probably won't anytime too soon:
http://processing.org/faq/platforms.html#java

if you're really wanting the 1.5 language stuff, you should use eclipse or something like that.. more info on that over in the "integration" board.
Re: Use of Comparator?
Reply #2 - Sep 13th, 2005, 5:58pm
 
I guess if I have JRE 5 installed it should work inside P5, or am I mistaken?

Well in case ever anybody looks into that topic again, you can solve it with a normal comparator:

Code:

import java.util.Arrays;  
import java.util.Comparator;  

int[][] test = new int[][]{   {1, 4}, {2, -8},  {3, 5}  };
     
       
   System.out.println( "unsorted:" );  
   for(int i=0;i<3;i++)
     print( test[i][0] + " > " + test[i][1] + "\n");  
       
   Arrays.sort( test, new Comparator()
   {
     public int compare( Object o1, Object o2 )
     {
       int[] a1 = (int[]) o1;
       int[] a2 = (int[]) o2;
       return a1[1] - a2[1] ;
     }
   }
   ); // sort
   
       
   System.out.println( "sorted:" );  
   for(int i=0;i<3;i++)
     print( test[i][0] + " > " + test[i][1] + "\n");

Re: Use of Comparator?
Reply #3 - Sep 14th, 2005, 4:45am
 
beachmeat wrote on Sep 13th, 2005, 5:58pm:
I guess if I have JRE 5 installed it should work inside P5, or am I mistaken


nope, not if i've already said it won't work and that you have to use eclipse. Wink

the preprocessor doesn't even understand that syntax, neither does jikes, the compiler that's used underneath.
Page Index Toggle Pages: 1