4 Replies Latest reply: Apr 1, 2008 11:12 AM by 800328 RSS

    2d array sorting....with double type values

    800328
      if there is an array
      double[][] Score = new double[3][2];
          Score[0][0] = 2.34;
          Score[0][1] = 2;
       
          Score[1][0] = 4.56;
          Score[1][1] = 7;
       
          Score[2][0] = 3.45;
          Score[2][1] = 1;
      How can i sort the array in ascending order, according to the values in first column(Double)..
      I have encountered the method called "Compare" but, it works only with int values...
      So, any idea????


      here is my code
      import java.util.*;
      class sort{
       
       
             public static void main(String[] args) {
          double[][] names = new double[3][2];
          names[0][0] = 2.34;
          names[0][1] = 2;
       
          names[1][0] = 4.56;
          names[1][1] = 7;
       
          names[2][0] = 3.45;
          names[2][1] = 1;
          for(int i = 0; i < names.length; i++) {
            System.out.println(names[0] + ", " + names[i][1]);
      }

      java.util.Arrays.sort(names, new java.util.Comparator<double[]>() {
      public double compare(double[] o1, double[] o2) {
      if((o1[0]) > (o2[0])) {
      return 1;
      } else if((o1[0]) < (o2[0])) {
      return -1;
      } else {
      return 0;
      }
      }

      }
      );
      System.out.println("after sorting");
      for(int i = 0; i < names.length; i++) {
           
      System.out.println(names[i][0] + ", " + names[i][1]);
      }

      }

           
           
      }
      and here are the errors i am getting
      C:\Documents and Settings\Sumit\Desktop>javac sort.java
      sort.java:20: <anonymous sort$1> is not abstract and does not override abstract
      method compare(double[],double[]) in java.util.Comparator
      public double compare(double[] o1, double[] o2) {
      ^
      1 error