English 中文(简体)
根据最后一栏排列
原标题:Sort according to last column
  • 时间:2012-01-12 15:31:37
  •  标签:
  • java
  • sorting

我有这样的阵列:

 UserID |ItemID | Score
    1      10     2.0
    1      11     1.2
    1      12     1.4

......

我想按照记分栏对阵列进行分类。 我的法典是:

double[][] arr=new double [1000][3];

    int i=0;int j=0;int k=0;
    while ((phrase = br.readLine()) != null) {
         String[] splited =phrase.split("	");
         arr[i][0]=Double.parseDouble(splited[0]);
         i++;
         arr[j][1]=Double.parseDouble(splited[1]);
         j++;
         arr[k][2]=Double.parseDouble(splited[2]);
         k++;
    }


    //print(a);
    System.out.println();
   Arrays.sort(arr, new java.util.Comparator<double[]>() {
       public int compare(double[] a, double[] b) {
            return (int)(a[1] -b[1]);
        }
    });
    print(arr);

但是,这并不奏效! 什么是错的?

最佳回答

如果您需要按第三栏分类,则需要使用<代码>a/ <>>。

public static void main(String[] args) {
    double[][] arr = { { 1, 10, 2.0 }, { 1, 11, 1.2 }, { 1, 12, 1.4 }, };
    ;
    Arrays.sort(arr, new Comparator<double[]>() {
        public int compare(double[] o1, double[] o2) {
            return Double.compare(o1[2], o2[2]);
        }
    });

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length; j++) {
            System.out.print(arr[i][j]);
            System.out.print("	");
        }
        System.out.println();
    }

}
问题回答

如果您希望按last分类。 页: 1 因此,请登录<代码>return(int)(a) ;,而不是return (int)(a ***-b l);

页: 1 为什么不只使用一种,例如i?

重新思考比较时,你不应使用<条码>(int)(a)1-2(b)),而应使用<条码>(Double.compare(a 1-2),如@Prashant Bhate所建议的。

原因是,如果两种数值之间的差别小于1或大于1,那么<代码>int<<>>>>>的条目将产生0,即两者均平等。 例:(int)(0.5 - 1.4)=(int)(-0.9)=0

这是因为记分栏是表第3栏——你在第二栏——即:

    Arrays.sort(arr, new java.util.Comparator<double[]>() {
    public int compare(double[] a, double[] b) {
        return (int)(a[2] - b[2]);
    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签