sorting by a alphabetical order with two string properties

Hi,

I'm new to this sorting stuff. Figured out the point of the Comparator interface. Now I'm wondering how to implement the simple logic of figuring out which string comes lower in the alphabet.

For example, two strings are cat and dog, cat comes first but how do I test for that in java? thanks

[318 byte] By [krebsneta] at [2007-10-2 2:02:27]
# 1

[snip]

> For example, two strings are cat and dog, cat comes

> first but how do I test for that in java? thanks

You ask the String class to do this test, and after looking at the JavaDoc for that class you see the various "compareTo" methods provided and you call one of those.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Good Luck

Lee

tsitha at 2007-7-15 19:43:49 > top of Java-index,Core,Core APIs...
# 2
See String.compareTo() method: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)
jbisha at 2007-7-15 19:43:49 > top of Java-index,Core,Core APIs...
# 3
To compare Strings alphabetically rather than by "ASCII"-code, use a Collator (java.text.Collator).
Herko_ter_Horsta at 2007-7-15 19:43:49 > top of Java-index,Core,Core APIs...
# 4

String c = "cat";

String d = "dog";

int i = d.compareTo(c) ;

String result = i<0?d+" first":i>0?c+" first":"equal";

System.out.println(result);

killerCodingNinjaMonkeya at 2007-7-15 19:43:49 > top of Java-index,Core,Core APIs...