Tuesday, November 19, 2002 03:39 AM
The order being lost is because the structure you're using (Properties) doesn't guarantee maintaining the order...
Basically you need to store key-value pairs in an ordered way. The simplest way would be to do something like this:
// an ordered structure for storing key-value pairs
Collection collection = new ArrayList();
ResultSet rs = ...;
MyKeyValuePair pair = new MyKeyValuePair(
Basically, the MyKeyValuePair class stores two strings, key and value. This class could also be replaced by using a two-cell String array with the agreement that the first cell contains the key and the second contains the value.
This approach is good for maintaining the order in the case of the list being accessed in order (i.e. in a loop).
However, if you need to access the values in a random order ("myMap.getValue(someKey)"), you need to implement double-mapping where the key-value pairs are stored both into an ordered structure (Collection, List, etc.) and a map-like structure (Map, Hashtable, Properties, etc.). Then you could access the individual key-value pairs quickly using the map structure, and still obtain an ordered list using the collection/list/whatever structure.