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

时间:2016-11-26 22:02:47

标签: java arrays sorting arraylist

例如我有一个数组:

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));

我想按照数字顺序对每种类似的ArrayList进行排序,根据每行中的第二个值,即1,3,1,5,2到1,1,2,3,5,同时保持其他变量在同一行完整。我不允许创建另一个类来按顺序存储这些变量。有谁知道我怎么能这样排序呢?

3 个答案:

答案 0 :(得分:0)

您使用了错误的数据结构。数组用于包含相同类型信息的相同类型 的多个变量

我建议你创建一个类并创建一个包含该类对象的数组,如下所示:

<强> 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());
            }

        });
    }
}

答案 1 :(得分:0)

如果将所有元素作为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);
}

答案 2 :(得分:0)

如果您创建一些有用的方法和比较器,您可以使用像bubble-sort这样的经典排序方法:

res/drawable/

更新:使用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[]