Thread: sort Strings

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Southern Germany
    Posts
    10
    Rep Power
    0

    Question sort Strings


    Hi,

    I'm working on a part of an application where you can choose a currency from a table. The table is filled via a method called "getAllCurrencies". each row contains the abbreviation as well as the currency's name.

    My problem is, the displayed 2-column table is unsorted so far.
    My predecessor simply reads the abbreviations into one String array and the names into another and then
    puts them together in a java.util.properties object.
    getAllCurrencies then returns the properties object.

    As I'm quite new to Java, I've no idea how to sort this, as I've read that the properties class itself doesn't provide a sort-method.
    Should I use another class instead of properties inside the method?
    It will still have to return a properties object, as I don't know if this method is called elsewhere.

    Thanks in advance,

    10_little_orcs
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Where do you get the abbreviations and currencys? Database or text file? If it's in a database, you can order them in the query. This would be easier.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Southern Germany
    Posts
    10
    Rep Power
    0
    Unfortunately I get the currencies via a remote command method from a text file on a distant server, so there is no query.
    And this remote command method is something I mustn't touch, because several other applications use it.
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    EDIT: realized a problem with my reply. You have 2 arrays that must stay in sync, so the sorting method I suggested will cause problems. There is probably a way to keep them in sync, but you will have to give it some thought. Below is my original reply.

    Second EDIT: It is unclear how the data is being stored in the Properties object. If you are indeed saving them as key/value pairs, then you may have to rethink your collection strategy. You may have to create your own sorted map (because map type collections like HashMap, HashTable, Properties, do not guarantee the order of the elements retrieved). Or you could use an ArrayList and store the data. Each "record" could be a 2 dimensional array of key/value and you would have to sort them before storing them, or use Collections.sort and create your own Comparable object to compare them. GL

    ----------------------------

    First of all, seems kind of odd to use a properties object to store the data in. This is not the usual use of this collection, however it should work and I understand you are inheriting someone elses code.

    One thing you can do is sort the arrays before putting them into the Properties collection. Since the String object implements the Comparable interface, it can be sorted with several premade sorting API's.

    In this case I would use the Arrays Object. Arrays.sort(Object[]) will sort an array of Strings in ascending order. This is a static method so you don't have to instantiate an Arrays object to use it. Run the array through this method before putting it in the Properties collection and you should be good to go.
    Last edited by Nemi; October 18th, 2002 at 10:19 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Southern Germany
    Posts
    10
    Rep Power
    0
    OK, that helps a lot - I didn't even know in which direction to turn.
    I already tried to sort the arrays but, as you mentioned, the properties object mixed everything up again.
    I will now try to use the ArrayList and create my own Comparable object.

    Thank you very much for your help!

IMN logo majestic logo threadwatch logo seochat tools logo