Sorting ArrayList

后端 未结 6 867
我在风中等你
我在风中等你 2021-01-25 19:19

Since I\'m just starting with JAVA, I\'m curious what is the best option for implementing sorting in JAVA (for ArrayLists). Below I provide my PHP code.

public i         


        
相关标签:
6条回答
  • 2021-01-25 19:57
    Collections.sort(a);
    

    Sources: Sorting an ArrayList

    0 讨论(0)
  • 2021-01-25 19:59
    for(int j = 0; j < myArray.size(); j++) {
            for (int i = j+1 ; i < myArray.size(); i++){
                if(myArray.get(i)[2].compareTo(myArray.get(j)[2]) < 0){
                    String[] temp = myArray.get(j);
                    myArray.set(j, myArray.get(i)); 
                    myArray.set(i, temp); 
                }
            }
        }
    

    I'm using the third field (myArray.get(j)[2]) for comparing. I hope this will help someone.

    0 讨论(0)
  • 2021-01-25 20:04

    Try this Way on you example :

    public static void main(String[] args) throws Exception {
            ArrayList<String[]> listOfStringArrays = new ArrayList<String[]>();
            listOfStringArrays.add(new String[] {"x","y","z"});
            listOfStringArrays.add(new String[] {"a","b","c"});
            listOfStringArrays.add(new String[] {"m","n","o"});
            Collections.sort(listOfStringArrays,new Comparator<String[]>() {
                public int compare(String[] strings, String[] otherStrings) {
                    return strings[1].compareTo(otherStrings[1]);
                }
            });
            for (String[] sa : listOfStringArrays) {
                System.out.println(Arrays.toString(sa));
            }
            /* prints out 
              [a, b, c]
              [m, n, o]
              [x, y, z]
            */ 
    
        }
    
    0 讨论(0)
  • 2021-01-25 20:04

    The simplest way to implement sorting in Java Collections is using the Collections#sort method.

    It uses a modified merge sort algorithm to do the job.

    It is important to say that it can only sort objects of a class that implements the Comparable interface, so you may need to take that into account. When you implement this interface you should know that it is best to think about the natural ordering of the object in question. e.g. Alphabetic order for Strings, if you need to sort it in an unnatural way on a specific context, do not use this interface.

    For that it's best if you define a Comparator when you invoke the method.

    0 讨论(0)
  • 2021-01-25 20:14

    The way to do this would be to use Collections class, and call Collections.sort(java.util.List, java.util.Comparator) on your list. It is documented here. The Comparator here is an interface that you would need to implement in order to do a custom sort. To implement the Comparator interface you need to provide implementations for

    int compare(T o1,T o2) and boolean equals(Object obj).

    using the logic you already have in your PHP file.

    0 讨论(0)
  • 2021-01-25 20:21

    Try this solution:

    public class SortArrayList{
        public static void main(String args[]){
    
            List<String> unsortList = new ArrayList<String>();
    
            unsortList.add("CCC");
            unsortList.add("111");
            unsortList.add("AAA");
            unsortList.add("BBB");
            unsortList.add("ccc");
            unsortList.add("bbb");
            unsortList.add("aaa");
            unsortList.add("333");
            unsortList.add("222");
    
            //before sort
            System.out.println("ArrayList is unsort");
            for(String temp: unsortList){
                System.out.println(temp);
            }
    
            //sort the list
            Collections.sort(unsortList);
    
            //after sorted
            System.out.println("ArrayList is sorted");
            for(String temp: unsortList){
                System.out.println(temp);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题