5 Replies Latest reply: Jan 24, 2008 11:57 AM by 807601

# compareTo; comparing two strings.

Hey guys,

A little help would be great.

I've been given the task of changing some code which orders and array of integers, into code which orders an array of strings.

The code I was given is:
``````public class exerciseThree {

public static void sort (int [] a) {

int min, temp;
for (int i=0; i<a.length-1; i++) {
min=i;
for (int j=i+1; j<a.length; j++)
if (a[j]<a[min]) min=j;
temp=a[min]; a[min]=a; a[i]=temp;
}
}
public static void main (String [] args) {
int[] a={14, 12, 29, 1, 3, 4, 1, 17, 29};
for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.print("\n");
sort(a);
for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.print("\n");
}
}And the code I have so far is:public class exerciseThreeFour {

public static void sort (String [] a) {

String temp;
for (int i=0; i<a.length-1; i++) {
String x="";
String min=x;
for (int j=i+1; j<a.length; j++)
// if (a[j].compareTo(a[min]), a<=0) min=(j)); //this is the main trouble area
if (a.compareTo(a[j], a[min])); //and this
temp=a[min]; a[min]=a[i]; a[i]=temp; //and this

// if (a[j]<a[min]) min=j;
// temp=a[min]; a[min]=a[i]; a[i]=temp;
}
}
public static void main (String [] args) {
String[] a={"CCGG", "CGCC", "GGCC", "GCCG", "GCCC", "GGGC", "CGGC", "GCGC", "CCCG"};
for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.print("\n");
sort(a);
for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.print("\n");
}
}We HAVE TO use the CompareTo method, but it doesnt seem to want to work.

I'm not asking someone to write my code, just a pointer/advice would be appreciated.

Cheers

Aaron.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ``````
• ###### 1. Re: compareTo; comparing two strings.
`` if (a[j]<a[min]) min=j;``
comapred to
``if (a.compareTo(a[j], a[min]));  ``
Well, first off in the second you forget to assign to min.

Secondly, in Java if always has a boolean condition and compareTo for Strings is a method which takes one String and returns an int:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#compareTo(java.lang.String)

so you need to compare that int to something to get a boolean (e.g. a[j].compareTo(a[min]) < 0 )
• ###### 2. Re: compareTo; comparing two strings.
hey,

thanks for that man.

I've changed it to
``````public class exerciseThreeFour {

public static void sort (String [] a) {

String temp;
for (int i=0; i<a.length-1; i++) {
String x="";
String min=x;
for (int j=i+1; j<a.length; j++)
if (a[j].compareTo(a[min])) == (-4); //illegal start of expression...
temp=a[min]; a[min]=a; a[i]=temp;
}
}
public static void main (String [] args) {
String[] a={"CCGG", "CGCC", "GGCC", "GCCG", "GCCC", "GGGC", "CGGC", "GCGC", "CCCG"};
for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.print("\n");
sort(a);
for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
System.out.print("\n");
}
}but have the illegal start of expresion error.

I really hate Java

thanks anyway bro.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ``````
• ###### 3. Re: compareTo; comparing two strings.
`` if (a[j]<a[min]) min=j;``
comapred to
``if (a.compareTo(a[j], a[min]));  ``
compared to
``if (a[j].compareTo(a[min])) == (-4); ``
Think about what the first bit of code does, It compares the j'th element if a with the min'th element of a, and if the j'th is less than the min'th, makes min equal to j.

In the first one, you keep track of indexes to the array, so you will want to do that, and you want to use compareTo to do a comparison, more like
``````if (a[j].compareTo(a[min]) < 0)
min=j;``````
• ###### 4. Re: compareTo; comparing two strings.
not workin either.

thanks man, but fuck it
• ###### 5. Re: compareTo; comparing two strings.
Why not use Arrays#sort()? It's part of the standard library. You can sort arrays of anything, primitive types, Objects, etc.