Results 1 to 7 of 7
Thread: Need help, prob. a dumb question
- 01-01-2011, 03:36 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
Need help, prob. a dumb question
Hey guys, I am new here and I have a program to do for my AP comp. science class, I am supposed to sort the values of my array ascending from A-Z it is a database, so I want to sort the objects in there alphabetically. Now I was thinking maybe I would convert the first letter of the name into an ascii value then compare the names that way? But then I did not know how to do that. Also I have to do this without using array.sort. (I know lame) anyway, I know it can be done, just am not fully understanding how it should be done. Any help would be greatly appreciated. Thanks!
- 01-01-2011, 04:49 AM #2
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 11
Using array.sort is probably the best with you can do, but if you run out of memory you should use quick-sort.
When converting to ASCII-value (not a correct term, but I'll use it for your sake right now) make sure you handle lower case and upper case correctly, for instance you can convert the strings to lower case (str.toLowerCase()). Converting to ASCII-value will only allow you to (easily) convert plane 0 Unicode (BMP) text correctly, which is good enough almost always, so it will do in your case, also you will not be (easily) able to handle diacritics (e.g. ` ´ ^ ~) correctly ("façade" will be inserted somewhere after faz).
To get the 0:th character in a String, str, you write
str.charAt(0)
The 'char' type is an unsigned 16-bit integer so they may be handled as integers.Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 01-01-2011, 05:25 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
here's one way without using Arrays
Java Code:import java.text.Collator; import java.util.Locale; public class StringCompare { public static void main(String[] args){ String[] s = {"TEST1" ,"abc" , "def" ,"Abc" , "Dfe" , "test" }; // choose your locale here. String[] result = sort( Collator.getInstance(Locale.US) , s ); for(int i=0; i< result.length; i++){ System.out.println( result[i] ); } } public static String[] sort(Collator col, String[] a) { String temp; if (a.length == 1) return a; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { // ascending order if( col.compare(a[i], a[j] ) > 0 ) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return a; } }
- 01-01-2011, 08:30 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 26
- Rep Power
- 0
To the people that posted -
Would it be easier to assign a numerical value to a given String based on its characters, using String.charAt(i), and then use a sorting algorithm? Or would this be inefficient?
- 01-01-2011, 09:31 AM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 11
- 01-01-2011, 08:37 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 26
- Rep Power
- 0
I meant something like:
First, sort by first character, e.g a = 1... z = 26.
If there is more than one character, sort by second... and so on.
Alternatively, I was also thinking of maybe converting all letters in a String to numerical values, like this:
Java Code:for(int i = 0; i < String.length; i++){ value += convertToNum(charAt(i))*10^(String.length-i-1) } //convert to num method, as explained in the 2nd line of my post //sort by value
- 01-03-2011, 10:59 AM #7
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 11
With
Java Code:10^(String.length-i-1)
10 xor (String.length-i-1).
Just want to point out that ^ is not an arithmetic operator, but an bitwise operator.Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
Similar Threads
-
I feel dumb asking this... File reader loop
By Adomini in forum New To JavaReplies: 5Last Post: 10-30-2010, 02:18 PM -
Real Newbie Question - I'm sure its dumb
By rulian in forum New To JavaReplies: 4Last Post: 12-11-2008, 05:07 PM -
Dumb Netbeans Q
By carderne in forum New To JavaReplies: 23Last Post: 05-28-2008, 04:37 AM -
for each prob
By kusumathatavarthi in forum New To JavaReplies: 7Last Post: 05-21-2008, 02:00 PM -
i am a dumb dumb
By goose_01ca in forum New To JavaReplies: 4Last Post: 04-25-2008, 05:44 AM
Bookmarks