繁体   English   中英

按List中的指定值对ArrayList进行排序

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM