如果你创建了一些有用的方法和比较,你可以使用一个经典的排序方法像冒泡排序:
public static void main(String[] args) {
String[] array1 =
{"15", "1", "D1", "Wine", "1", "0",
"15", "3", "D3", "Tap water", "2", "2",
"15", "1", "M1", "Fish", "3", "0",
"9", "5", "D4", "Coffee", "2", "2",
"9", "2", "P2", "Cake", "2", "1"
};
Comparator<String[]> comparator = new Comparator<String[]>(){
@Override
public int compare(String[]a1, String[] a2) {
return Integer.valueOf(a1[1]).compareTo(Integer.valueOf(a2[1]));
}
};
int lineLength=6;
bubbleSort(array1,lineLength,comparator);
System.out.println(Arrays.toString(array1));
}
//classic bubble-sort algorithm
public static void bubbleSort(String[]array1,int lineLength,Comparator<String[]> comparator){
int numRow=array1.length/lineLength;
for(int i=0;i<numRow;i++){
for(int j=i+1;j<numRow;j++){
String[] extractArrayI = extractArray(array1, i, lineLength);
String[] extractArrayJ = extractArray(array1, j, lineLength);
if(comparator.compare(extractArrayI, extractArrayJ)>0){
swichLines(array1,i,j,lineLength);
}
}
}
}
//extract i-th row
public static String[] extractArray(String[]array,int i, int lineLength){
String [] a= new String[lineLength];
System.arraycopy(array, i*lineLength, a, 0, lineLength);
return a;
}
//Switch line i,j
public static void swichLines(String[]array,int i, int j,int lineLength){
String [] temp = new String[lineLength];
System.arraycopy(array, i*lineLength, temp, 0, lineLength);
System.arraycopy(array, j*lineLength, array, i*lineLength, lineLength);
System.arraycopy(temp, 0, array, j*lineLength, lineLength);
}
更新:使用List<String>
代替String[]
:
public static void main(String[] args) {
String[] array1 =
{"15", "1", "D1", "Wine", "1", "0",
"15", "3", "D3", "Tap water", "2", "2",
"15", "1", "M1", "Fish", "3", "0",
"9", "5", "D4", "Coffee", "2", "2",
"9", "2", "P2", "Cake", "2", "1"
};
List<String> list = Arrays.asList(array1);
Comparator<List<String>> comparator = new Comparator<List<String>>(){
@Override
public int compare(List<String>a1, List<String> a2) {
return Integer.valueOf(a1.get(1)).compareTo(Integer.valueOf(a2.get(1)));
}
};
int lineLength=6;
System.out.println(list.toString());
bubbleSort(list,lineLength,comparator);
System.out.println(list.toString());
}
//classic bubble-sort algorithm
public static void bubbleSort(List<String> list,int lineLength,Comparator<List<String>> comparator){
int numRow=list.size()/lineLength;
for(int i=0;i<numRow;i++){
for(int j=i+1;j<numRow;j++){
List<String> extractArrayI = extractArray(list, i, lineLength);
List<String> extractArrayJ = extractArray(list, j, lineLength);
if(comparator.compare(extractArrayI, extractArrayJ)>0){
swichLines(list,i,j,lineLength);
}
}
}
}
//extract i-th row
public static List<String> extractArray(List<String> list,int i, int lineLength){
return list.subList(i*lineLength, i*lineLength+lineLength);
}
//Switch line i,j
public static void swichLines(List<String>list,int i, int j,int lineLength){
List<String>tempI = new ArrayList<String>(list.subList(i*lineLength, i*lineLength+lineLength));
List<String>tempJ = new ArrayList<String>(list.subList(j*lineLength, j*lineLength+lineLength));
replaceSublist(list,tempJ,i,lineLength);
replaceSublist(list,tempI,j,lineLength);
}
//replace sublist
private static void replaceSublist(List<String> list, List<String> temp, int line, int lineLength) {
for (int k=0; k<lineLength; k++)
{
list.set(line*lineLength+k, temp.get(k));
}
}
嗨!你试过什么了?你可以把这个问题呢? –
看起来你正在使用错误的数据结构 –
你的“行”在你的数据结构中没有被标记为这样 - 把它变成一个行数组(首先有一些“行”类型,例如数组)会使这个变得更简单。那么你只需要实现一个比较器,并且可以使用标准的排序功能。 –