[英]Sorting ArrayList by specified values in List
例如,我有一個數組:
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"
};
someList.addAll(Arrays.asList(Array1));
我想根據每行中的第二個值(即1,3,1,5,2到1,1,2,3,5)對數字順序排序的某種類型的ArrayList進行排序,同時保持其他變量相同線完好無損。 我不允許創建另一個類來按順序存儲這些變量。 有誰知道我怎么能這樣排序他們?
您使用了錯誤的數據結構。 數組用於包含相同類型信息的同一類型的多個變量。
我建議你創建一個類並創建一個包含該類對象的數組,如下所示:
Drink.class
class Drink{
private int a;
private int b;
private String c;
private String drinkName;
private int d;
private int e;
public Drink(int a,int b,String c,String drinkName,int d,int e){
this.a=a;
this.b=b;
this.c=c;
this.drinkName=drinkName;
this.d=d;
this.e=e;
}
// Getters and setters here
public String getDrinkName(){
return drinkName;
}
// .....
}
然后在你的主要課程中:
class MainClass{
List<Drink> drinks;
public static void main(String[] args){
drinks = new ArrayList<>();
drinks.add(new Drink(15,1,"D1","Wine",1,0));
drinks.add(new Drink(15,3,"D3","Tap Water",2,2));
// Etc...
// You can retrieve elements using .get(index) and then use getters on it to retrieve informations
System.out.println(drinks.get(0).getDrinkName());
Collections.sort(drinks,new Comparator<Drink>(){
@Override
public int compare(Drink d1, Drink d2){
// Edit this method as you need
return d1.getA().compareTo(d2.getA());
}
});
}
}
如果將所有元素作為String
值存儲到array
則無法輕松對元素進行排序。 相反,您可以使用OOP並定義名為MyCustomData
的自定義類型(類),然后將數據作為對象加載。
因此,您需要按照以下步驟操作:
(1)定義自定義類MyCustomData
(2)為MyCustomData
創建對象並將它們加載到數組中。
(3)現在,使用Comparator
對數組進行排序
您可以參考以下代碼並注釋:
MyCustomData類(正確命名此類):
public class MyCustomData {
private int value1;//holds your element to be sorted
//other values //define other values to hold fish, etc..
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
}
對MyCustomData數組進行排序:
public static void main(String[] args) {
MyCustomData[] myCustomDataArray = new MyCustomData[5];
MyCustomData myCustomData1 = new MyCustomData();
myCustomData1.setValue1(1);
myCustomDataArray[0] = myCustomData1;
//Create and Load other objects myCustomDataArray[1] , [2], ....into array
Comparator<MyCustomData> comp = (MyCustomData data1, MyCustomData data2)
-> data1.getValue1()-data2.getValue1();
Arrays.stream(myCustomDataArray).sorted(comp);
}
如果您創建一些有用的方法和比較器,您可以使用像bubble-sort這樣的經典排序方法:
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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.