I have two arrays one for temperatures and one for the cities.If two temperatures are the same the corresponding cities must be shown with alphabetical sort order.For instance
- 24 Sydney
- 22 Athens
- 22 Auckland
- 20 Barcelona
--->This is the sort code.
public static void sort(String[] x , double[] y) {
boolean doMore = true;
while(doMore) {
doMore = false;
for(int i=0; i< x.length-1; i++) {
if(y[i] == y[i+1] && x[i] < x[i+1]) {
String temp2 = x[i];
x[i] = x[i+1];
x[i+1] = temp2;
} else if (y[i] < y[i+1]) {
double temp = y[i];
y[i] = y[i+1];
y[i+1] = temp;
String temp2 = x[i];
x[i] = x[i+1];
x[i+1] = temp2;
doMore = true;
}//if
}//for 2
}//while
}// sort */
The problem is that on the if(y[i] == y[i+1] && x[i] < x[i+1])
i can't just compare 2 stings with the operator >
. I can't use any ready sort methods either.
Does anyone know how to sort tho strings alphabetical there?
compareTo
? E.g.if(str1.compareTo(str2) > 0)
– cowls Dec 8 '11 at 17:15