0

Java how to sort a list of array: List<String[]>

List<String[]> rows=new ArrayList<String[]>();

Collections.sort(rows);

Collections.sort(rows) gives me the following errors:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<String[]>). 
The inferred type String[] is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Changwang Zhang
  • 2,467
  • 7
  • 38
  • 64
  • 4
    How do you want to sort it? According to what? Do you want the array to be sorted as well? – amit May 20 '14 at 14:34
  • Java does not implement array comparison by default. With what criterion do you wish your arrays to be sorted? – E_net4 May 20 '14 at 14:35

2 Answers2

4

Well, this is not a standard sort according to the natural order of the objects, because there is no natural order of arrays.

You might be looking for Collections.sort(rows,comparator), where comparator is your custom Comparator objects, that tells you "which array is bigger".
The implementation of the Comparator is obviously dependent on the order you want.

amit
  • 175,853
  • 27
  • 231
  • 333
0

You have to use a comparator.

class StringArrayComparator implements Comparator<String[]> {

    public int compare(String[] s1, String[] s2) {
        // Conditions here.
    }
}

Then plug it into the Collections call:

Collections.sort(rows, new StringArrayComparator());

This will sort the String[] list according to the criteria in the comparator.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75