Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 06, 2012 Ordering an associative array - or - another option | ||||
---|---|---|---|---|
| ||||
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! |
June 06, 2012 Re: Ordering an associative array - or - another option | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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.
|
June 06, 2012 Re: Ordering an associative array - or - another option | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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
|
June 08, 2012 Re: Ordering an associative array - or - another option | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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...
|
Copyright © 1999-2021 by the D Language Foundation