再web板块看到的,没人解决,大家帮忙看看有没有什么好的解决方法
写一个java程序,实现对一个二维数组按指定的列集进行排序?要求实现类似sql中order by的功能,移动时,整行移动,不能打乱整行顺序。
可将二维数组想象成数据库里的一个表记录集,然后按指定的列集进行排序,即order by col1,col2。
//a为二维数组,sortCols是要按哪几列排序,如0,2,以逗号分隔,升序排。
public void sortIntArrs(int[][] a,String sortCols)
{
}
写一个java程序,实现对一个二维数组按指定的列集进行排序?要求实现类似sql中order by的功能,移动时,整行移动,不能打乱整行顺序。
可将二维数组想象成数据库里的一个表记录集,然后按指定的列集进行排序,即order by col1,col2。
//a为二维数组,sortCols是要按哪几列排序,如0,2,以逗号分隔,升序排。
public void sortIntArrs(int[][] a,String sortCols)
{
}
public class test {
public static void main(String[] arg)throws Exception {
int ary[][]={{2,5,1,0,6},{6,9,4,8,5},{3,6,4,7,5},{2,3,1,5,7}};
sort2 s=new sort2("0213");
Arrays.sort(ary,s);Arrays.sort(ary,s);
for(int[] a:ary) {
for(int 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){ss=s+"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;
}
}
{ int [] is = null;
Comparator c = null;
public ComparatorImpl(int[] is)
{
this.is = is;
}
public ComparatorImpl(int[] is,Comparator c)
{
this.is = is;
this.c = c;
}
public int compare(Object o1, Object o2)
{
Object[] os1 = (Object[])o1;
Object[] os2 = (Object[])o2;
int compareInt = 0;
for (int i = 0; i < is.length ; i++)
{
compareInt = comp(os1[i],os2[i]);
if (compareInt != 0)
{
return compareInt;
}
}
return compareInt;
}
private int comp(Object o1,Object o2)
{
//
return 0;//FIXME
}
}