Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formating a String-Array with more than one dimension

 
Ben Klug
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
how can i sort this array 'o' by the first colmn??
//********************************
String o[][] = new String[3][2];
o[0][0]="1111111111";
o[0][1]="eins";
o[1][0]="0000000000";
o[1][1]="zwei";
o[2][0]="2222222222";
o[2][1]="drei";
for (int i = 0; i < o.length ; i++){
System.out.println(o[i][0] + "||" + o[i][1]);
}
//********************************
thanks in advance
Ben
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is to create a java.util.Comparator which compares the first entries in two String[] arrays. Then use java.util.Arrays.sort(Object[], Comparator) to sort the array quickly, with minimal fuss. This may a bit advanced for the "Beginner" forum, but it's worth the effort to learn how Comparators work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic