Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-02-2003, 07:23 PM   #1 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
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 )
Apodysophilia is offline   Reply With Quote
Old 04-02-2003, 11:52 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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.
joe_bruin is offline   Reply With Quote
Old 04-03-2003, 05:56 AM   #3 (permalink)
sno2dude
Registered User
 
sno2dude's Avatar
 
Join Date: Apr 2003
Location: Michigan Tech
Posts: 38
sno2dude is on a distinguished road
Send a message via AIM to sno2dude Send a message via Yahoo to sno2dude
Cool

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.
sno2dude is offline   Reply With Quote
Old 04-03-2003, 07:12 AM   #4 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
thank you for the info.

i am probly gonna try sno2dude's way, becase i (sadly) dont know what vector classes are (YET:rock: )
Apodysophilia is offline   Reply With Quote
Old 04-03-2003, 07:20 AM   #5 (permalink)
sno2dude
Registered User
 
sno2dude's Avatar
 
Join Date: Apr 2003
Location: Michigan Tech
Posts: 38
sno2dude is on a distinguished road
Send a message via AIM to sno2dude Send a message via Yahoo to 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.

yeah, but my code should work as i just did it and wrote a test driver to test my code too...... word....... :rock:
sno2dude is offline   Reply With Quote
Old 04-03-2003, 10:14 AM   #6 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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);
}
joe_bruin is offline   Reply With Quote
Old 04-03-2003, 03:55 PM   #7 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
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
Apodysophilia is offline   Reply With Quote
Old 04-03-2003, 04:32 PM   #8 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
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.
abc123 is offline   Reply With Quote
Old 04-03-2003, 04:57 PM   #9 (permalink)
Apodysophilia
Regular Contributor
 
Apodysophilia's Avatar
 
Join Date: Apr 2003
Location: noWhere, PA
Posts: 104
Apodysophilia is on a distinguished road
Send a message via AIM to Apodysophilia
wow thanks
Apodysophilia is offline   Reply With Quote
Old 04-04-2003, 06:18 AM   #10 (permalink)
sno2dude
Registered User
 
sno2dude's Avatar
 
Join Date: Apr 2003
Location: Michigan Tech
Posts: 38
sno2dude is on a distinguished road
Send a message via AIM to sno2dude Send a message via Yahoo to sno2dude
*shrugs* i guess our professor just wants us to learn to do it the diffucult way so we become stronger programmers..... oh well....
sno2dude is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
combining arrays Engineer Standard C, C++ 11 03-12-2005 10:23 AM
Dynamic Arrays, the 'right' way. Mr.Anderson Standard C, C++ 10 10-05-2004 06:00 AM
sorting objects arrays by object properties sde Java 28 08-05-2004 05:51 AM
Need an alphabetic sort algorithm SkittlesAreYum Java 4 05-08-2003 11:02 PM


All times are GMT -8. The time now is 02:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting