Results 1 to 7 of 7
  1. #1
    Demetrius82 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Smile 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!

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    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.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Demetrius82 View Post
    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!
    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;
        }
    }
    read the documentation for Collator/Locale to find out more.

  4. #4
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    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?

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Lord Voldemort View Post
    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?
    but then what about subsequent characters?, eg "abt" and "abd" ? you can't just check the first character. Or am i not getting what you are saying.?

  6. #6
    Join Date
    Nov 2010
    Posts
    26
    Rep Power
    0

    Default

    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
    EDIT: I guess values > 9 would screw this up, nevermind.

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    With
    Java Code:
    10^(String.length-i-1)
    I assume you mean 10 to the power of (String.length-i-1), not
    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

  1. I feel dumb asking this... File reader loop
    By Adomini in forum New To Java
    Replies: 5
    Last Post: 10-30-2010, 01:18 PM
  2. Real Newbie Question - I'm sure its dumb
    By rulian in forum New To Java
    Replies: 4
    Last Post: 12-11-2008, 04:07 PM
  3. Dumb Netbeans Q
    By carderne in forum New To Java
    Replies: 23
    Last Post: 05-28-2008, 03:37 AM
  4. for each prob
    By kusumathatavarthi in forum New To Java
    Replies: 7
    Last Post: 05-21-2008, 01:00 PM
  5. i am a dumb dumb
    By goose_01ca in forum New To Java
    Replies: 4
    Last Post: 04-25-2008, 04:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •