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

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

我想根据每行中的第二个值(即 1,3,1,5,2 )对与此类似的某种类型的 ArrayList 按数字顺序排序为 1,1,2,3,5 ,同时保持同一行中的其他变量完好无损。我不允许创建另一个类来按顺序存储这些变量。有谁知道我该如何对它们进行排序?

最佳答案

如果您创建一些有用的方法和比较器,您可以使用经典的排序方法,例如冒泡排序:

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

关于java - 按 List 中的指定值对 ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40823617/

相关文章:

java - LibGDX 获取最大纹理大小 - 空指针异常

java - 使用 GSON 将字符串解析为 JsonObject 给出 IllegalStateException : This is not a JSON Object

java - 重新部署war时需要重启tomcat吗?

java - 在 HashMap 中使用字符串数组,Java

java - 过滤 Java 列表并创建整型常量数组

java - 最佳搜索给定区域中的 2D 点(网络服务)

ruby - 按数组中出现的频率排序

java - 尝试在 cmd 中运行我的项目时出错

C 语言的凯撒密码

jquery Datatables - 如何实现服务器端获取和客户端排序