您的意思是:
我必须能够按sij参数排序
有什么问题:
Object [][] data
编辑
好吧,只是猜测你需要的是一个“StrangeDataStructure”,它保存数组,并帮助你按第一列排序,那么你唯一需要的是这样的东西:
class Structure {
Object [][] data;
Object [] indexColumn; // the sij?
}
就是这样:你应该添加一个指示方向的排序方法,并使用“indexColumn”进行排序。
我认为VEEERY很简单(如果我理解了你的“问题” )
你知道吗?我将实现它。
时间已过...
在这里:
import java.util.Comparator;
import java.util.Arrays;
public class StrangeStructure {
private Integer [][] data;
private Integer [] sij; // what is sij anyway?
public StrangeStructure( Integer [][] matrix ) {
data = matrix;
sij = new Integer[ data.length ];
for( int i = 0 ; i < data.length ; i++ ) {
sij[i] = data[i][0];
}
}
public void sort( Direction direction ) {
Comparator sijComparator = new DataComparator( direction, true );
Comparator dataComparator = new DataComparator( direction, false );
Arrays.sort( sij, sijComparator );
Arrays.sort( data, dataComparator );
}
public static void main( String [] args ) {
StrangeStructure s =
new StrangeStructure( new Integer[][]{
{ 45, 5, 7 },
{ 33, 1, 6 },
{ 31, 0, 9 },
{ 12, 8, 2 }
});
System.out.printf("Original:\n%s", s );
s.sort( Direction.MIN_TO_MAX );
System.out.printf("Min to max:\n%s", s );
s.sort( Direction.MAX_TO_MIN );
System.out.printf("Max to min\n%s", s );
}
public String toString() {
StringBuilder b = new StringBuilder();
for( Integer [] row : data ) {
for( int i : row ) {
b.append( i+",");
}
b.append("\n");
}
return b.toString();
}
}
class DataComparator implements Comparator {
private Direction direction;
private boolean isSij;
public DataComparator( Direction d, boolean isSij ) {
this.direction = d;
this.isSij = isSij;
}
public int compare( Object one , Object two ) {
if( isSij ){
return doCompare( direction, (Integer) one, (Integer) two );
} else {
return doCompare( direction, ((Integer[])one)[0], ((Integer[])two)[0]);
}
}
public int doCompare( Direction d, int one, int two ) {
int a = ( d == Direction.MIN_TO_MAX? one: two );
int b = ( d == Direction.MIN_TO_MAX? two: one ) ;
return a - b;
}
public boolean equals( Object o ) {
return false;
}
}
enum Direction{
MIN_TO_MAX,
MAX_TO_MIN
}
输出:
Original:
45,5,7,
33,1,6,
31,0,9,
12,8,2,
Min to max:
12,8,2,
31,0,9,
33,1,6,
45,5,7,
Max to min
45,5,7,
33,1,6,
31,0,9,
12,8,2,