日期:2014-05-20  浏览次数:20671 次

Long型的数据进行排序的函数
我想做一个对Long型的数据进行排序的函数:如下


import   java.util.*;


public   class   test   {
public   static   void   main(String[]   arg)throws   Exception   {
Long   ary[][]={{27785727527278L,7857852544545455L},{675785785757857L,978787878787L},{378787878787L,78787877857876L},{5787878787L,878787878785L}};//测试数据
                                        //读入数据部分
sorttype   s=new   sorttype( "A ");//按A段排序
Arrays.sort(ary,s);//对二维数组排序
//创建写入对象
                                    //writetofile   file   =   new   writetofile();
for(Long[]   a:ary)   {
for(Long   i:a)  
System.out.print(i+ "| ");//测试输出
                                                                  System.out.println();

    }
  }
}

//自定义实现排列顺序
class   sort2   implements   Comparator <int[]> {
private   String   ss;
        private   int   ind=0;
        public   sort2(String   s){
        if(s== "A ")ss= "0 ";
        else   if(s== "B ")ss= "1 ";
        else   ss= "0 ";
}
       
        public   int   compare(int   a[],   int   b[])   {
        int   r;
                if(a[Integer.parseInt(ss.substring(ind,ind+1))]> b[Integer.parseInt(ss.substring(ind,ind+1))])
                r=1;
                else   if(a[Integer.parseInt(ss.substring(ind,ind+1))] <b[Integer.parseInt(ss.substring(ind,ind+1))])
                        r=-1;
                else   r=0;
                ind++;
                if(ind> =ss.length())   ind=0;
                return   r;
      }
}


提示错误:The   method   sort(T[],   Comparator <?   super   T> )   in   the   type   Arrays   is   not  
  applicable   for   the   arguments   (Long[][],   sorttype)

请问怎么改啊?


------解决方案--------------------
The method sort(T[], Comparator <? super T> ) in the type Arrays is not
applicable for the arguments (Long[][], sorttype)

因为sort()方法的第一个对数是一维数组,而你调用的时候给的ary[][]是二维数组,当然会出错,改成一维的应该就可以了。
------解决方案--------------------
二维数线[n][n]可以当做n个一维数组[n],每个数组的长度是n.
注意多维数组的每一维可能长度不一致。

import java.util.*;

class Cmpr implements Comparator {
public int compare(Object o1, Object o2) {