1

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 int cmp($a, $b) {
    if ( $a[0] < $b[0] ) return 1;
    else if ( $a[0] > $b[0] ) return -1;
    else if ( $a[1] < $b[1] ) return 1;
    else if ( $a[1] > $b[1] ) return -1;
    else return 0;
}

$selected = array();

for ($i=0; $i<$len; $i++) {
    $rank = getRank();
    $cub = getCub_len();
    $selected[] = array($rank,$cub);
}

uasort($selected, 'cmp')

Well, I wrote the following code in JAVA:

ArrayList<ArrayList<Double>> selected = new ArrayList<ArrayList<Double>>();
ArrayList<Double> rank = new ArrayList<Double>();
ArrayList<Double> cub = new ArrayList<Double>();

for (int i=0; i<len; i++) {
 rank.add(getRank(i));
 cub.add(getCub(i));
}

selected.add(0,rank);
selected.add(1,cub);

How to sort selected in the proper way (similarly to PHP function cmp)?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – Peter Perháč Mar 27 '13 at 18:21
  • The PHP code and Java code are doing different things. In the PHP code you end up with an array of arrays of `$len`, in the Java code you end up with an array of 2 arrays. – Danny Mar 27 '13 at 18:23
  • By the way, it's Java and not JAVA. Java is a word and not an acronym. – Steve Kuo Mar 27 '13 at 18:25

6 Answers6

6
Collections.sort(a);

Sources: Sorting an ArrayList

Chauer
  • 98
  • 4
3

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]
        */ 

    }
Community
  • 1
  • 1
Alya'a Gamal
  • 5,624
  • 19
  • 34
2

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);
        }
    }
}
Barney
  • 2,355
  • 3
  • 22
  • 37
Sajad
  • 2,273
  • 11
  • 49
  • 92
1
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.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
macmuri
  • 115
  • 7
0

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.

Rodrigo Sasaki
  • 7,048
  • 4
  • 34
  • 49
0

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.

Bizmarck
  • 2,663
  • 2
  • 33
  • 48