Sorting ArrayList

后端 未结 6 958
我在风中等你
我在风中等你 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: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.

提交回复
热议问题