|  | |  |
04-02-2003, 08:23 PM
|
#1 (permalink)
| | Regular Contributor
Join Date: Apr 2003 Location: noWhere, PA
Posts: 104
| sort arrays ok, i am a big java newbie(also alittle C++) and found this site:rock: , looked around a little and relized "wow, this could really help me become a better programmer."
I was wondering some stuff, if i have an array of strings, how can i put then in alphabetical order, ether out put them, or put them back in the array in alphabetical order?, any help (sample code would be appreciated  ) |
| |
04-03-2003, 12:52 AM
|
#2 (permalink)
| | LOAD "*",8,1
Join Date: Feb 2003 Location: la.ca.us
Posts: 254
| sorting is one of the classic problems of computer science. if you want to implement a sort yourself, i suggest you google for "quicksort" or "mergesort".
java, however, provides some nice tools that will do the job for you. you could put the strings into a Vector, SortedMap, or one of several other structures provided by the java class library, that can do the sorting for you. hint: look at the Comparable interface. |
| |
04-03-2003, 06:56 AM
|
#3 (permalink)
| | Registered User
Join Date: Apr 2003 Location: Michigan Tech
Posts: 38
| or, if you dont wanna deal with the pain in the ass vector class. You can compare strings because they are comparable objects. i just had to do this for a project and it works quite well.
what you do is something like this where T is an array of srtings
for(int i=0; 1<T.length;i+=1){
index = i;
start = index + 1;
while(start<T.length){
int w = T[index].compareTo(T[start]);
if(w>0){
String temp=T[index];
T[index]=T[start];
T[start]=temp;
}
start += 1;
}
}
this will return T sorted by alphabetical order decending. Im pretty sure, i dont have my code here but that is what it is off the top of my head. the compareTo method returns 0 if two Strings are equal, a negative number if the first string precedes the second string alphabetically and a positive number if the second string precedes the first string alphabetically. It does this by comparing the ASCII values of the letters at each position til it (a) runs out of letters in one or both strings or (b) finds a point where they are different. If you want to sort it decending, then you just have to change w>0 to w<0. |
| |
04-03-2003, 08:12 AM
|
#4 (permalink)
| | Regular Contributor
Join Date: Apr 2003 Location: noWhere, PA
Posts: 104
|  thank you for the info.
i am probly gonna try sno2dude's way, becase i (sadly) dont know what vector classes are  (YET:rock: ) |
| |
04-03-2003, 08:20 AM
|
#5 (permalink)
| | Registered User
Join Date: Apr 2003 Location: Michigan Tech
Posts: 38
| a vector is like an array except that its size changes every time that you add something to it. I dont use it because you have to cast things to put it into a vector and convert them to take it out and do a bunch of other things with it so it will work right.... that and i fell asleep halfway through the class on vectors.
yeah, but my code should work as i just did it and wrote a test driver to test my code too...... word....... :rock: |
| |
04-03-2003, 11:14 AM
|
#6 (permalink)
| | LOAD "*",8,1
Join Date: Feb 2003 Location: la.ca.us
Posts: 254
| Quote: Originally posted by sno2dude a vector is like an array except that its size changes every time that you add something to it. I dont use it because you have to cast things to put it into a vector and convert them to take it out and do a bunch of other things with it so it will work right.... that and i fell asleep halfway through the class on vectors. | what you implemented is known as bubblesort. it is one of the easiest sorting algorithms to understand (and code). it is also one of the slowest.
you don't have to convert any objects to put them into a vector, since vectors take Object types, and all classes inherit from Object. casting is very simple and safe in java, and is commonly performed in most API data structures. if you have problems with casting, java may not be your thing.
having said that, it seems that i was wrong about vector (i thought it has a sort call, but it doesn't). instead, try the TreeSet class. the code below is untested, but probably comes close to working. Code: TreeSet ts = new TreeSet();
ts.add("hi");
ts.add("dude");
ts.add("asdf");
Iterator it = ts.iterator();
while(it.hasNext())
{
String st = (String)it.next();
System.out.println(st);
} |
| |
04-03-2003, 04:55 PM
|
#7 (permalink)
| | Regular Contributor
Join Date: Apr 2003 Location: noWhere, PA
Posts: 104
| wow, thanks for the info. looks like i'm gonna have to learn trees  and a HUGE thanx for the sample code and an explation  |
| |
04-03-2003, 05:32 PM
|
#8 (permalink)
| | bloomberg
Join Date: Jun 2002 Location: bloomberg
Posts: 263
| don't really have to bother with trees etc to get java to sort for you: Code: String[] hmm = {"abc", "def", "aaa", "bbb", "zzz"};
java.util.Arrays.sort(hmm);
//... done ...
__________________ -- bloomberg. |
| |
04-03-2003, 05:57 PM
|
#9 (permalink)
| | Regular Contributor
Join Date: Apr 2003 Location: noWhere, PA
Posts: 104
| wow thanks |
| |
04-04-2003, 07:18 AM
|
#10 (permalink)
| | Registered User
Join Date: Apr 2003 Location: Michigan Tech
Posts: 38
| *shrugs* i guess our professor just wants us to learn to do it the diffucult way so we become stronger programmers..... oh well....  |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 12:13 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |