www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ordering an associative array - or - another option

reply "Paul" <phshaffer gmail.com> writes:
I have and array string[string][string][string] that works great 
for everything I need except that they (assoc. arrays) don't 
maintain an order.  I need to maintain the order of entry.

Are there any work arounds that others have used?  I saw some 
tricks in the book for sorting a single dimensional assoc. array.

Thanks to all!
Jun 06 2012
next sibling parent "simendsjo" <simendsjo gmail.com> writes:
On Wednesday, 6 June 2012 at 14:04:17 UTC, Paul wrote:
 I have and array string[string][string][string] that works 
 great for everything I need except that they (assoc. arrays) 
 don't maintain an order.  I need to maintain the order of entry.

 Are there any work arounds that others have used?  I saw some 
 tricks in the book for sorting a single dimensional assoc. 
 array.

 Thanks to all!

As you say, AAs are unordered. Instead of a string, you could use a struct that contains a position as well as the string and sort before you need to traverse the values in order.
Jun 06 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Wednesday, June 06, 2012 16:04:14 Paul wrote:
 I have and array string[string][string][string] that works great
 for everything I need except that they (assoc. arrays) don't
 maintain an order. I need to maintain the order of entry.
 
 Are there any work arounds that others have used? I saw some
 tricks in the book for sorting a single dimensional assoc. array.
 
 Thanks to all!

If you want an ordered map, then use std.container.RedBlackTree. It's a little bit annoying to use as a map (you basically have to use it as a set of pairs/tuples which are ordered on their first member), but it's quite doable (and is what C++'s STL does internally with std::map). Chaining them like you seem to be trying to do though would probably get ugly though. - Jonathan M Davis
Jun 06 2012
prev sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Wednesday, 6 June 2012 at 17:05:35 UTC, Jonathan M Davis wrote:
 On Wednesday, June 06, 2012 16:04:14 Paul wrote:
 I have and array string[string][string][string] that works 
 great for everything I need except that they (assoc. arrays) 
 don't maintain an order. I need to maintain the order of entry.

 Are there any work arounds that others have used? I saw some 
 tricks in the book for sorting a single dimensional assoc. 
 array. Thanks to all!

If you want an ordered map, then use std.container.RedBlackTree. It's a little bit annoying to use as a map (you basically have to use it as a set of pairs/tuples which are ordered on their first member), but it's quite doable (and is what C++'s STL does internally with std::map). Chaining them like you seem to be trying to do though would probably get ugly though.

Agreed, it would get ugly and looks like difficult right off the bat, since RedBlackTree's are classes you'll need to initialize them on each level, or have a general purpose one with two purposes and they chain internally... Hmmm... maybe...
Jun 07 2012