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,540
  • 9
  • 46
  • 78
Changwang Zhang
  • 2,427
  • 5
  • 34
  • 61

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
  • 172,148
  • 26
  • 225
  • 324
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,166
  • 6
  • 49
  • 73