October 17th, 2002, 02:58 AM
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,
October 17th, 2002, 01:46 PM
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.
October 18th, 2002, 02:49 AM
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.
October 18th, 2002, 09:54 AM
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.
October 18th, 2002, 12:43 PM
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!