www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sort keys in an associative array?

reply clayasaurus <clayasaurus gmail.com> writes:
Can I sort the key order in an associative array?

It doesn't work with this example below...

////////////////////////////////////////////////////////
//Ex: Trying to sort assoc array key order
////////////////////////////////////////////////////////
import std.stdio;

int main()
{


    int[int] array;

    array[0] = 3;
    array[100] = 1;
    array[4] = 10;

    array.keys.sort;

    // i want the new key order to be 0, 4, 100

    foreach(int key; array.keys)
    {
       writefln("key(",key,")");
    }

    // prints 0, 100, 4

    return 0;
}

Thanks ahead to anyone who knows the answer.

~ Clay
Sep 11 2005
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"clayasaurus" <clayasaurus gmail.com> wrote in message 
news:dg1io2$2mg2$1 digitaldaemon.com...
 Can I sort the key order in an associative array?

 It doesn't work with this example below...

The AA.keys property only returns an array that is a representation of the keys in the AA; thus, sorting the array returned by AA.keys does not affect the original AA. Since the AA is laid out in a nonlinear format, it wouldn't really be possible to sort the key-value pairs. What you can do, however, is get the keys array, sort it, and loop through it, accessing the values in the AA by using the keys array. Like so: import std.stdio; void main() { int[int] array; array[0] = 3; array[100] = 1; array[4] = 10; int[] keys = array.keys; keys.sort; foreach(int i; keys) { writefln("array[",i,"] = ",array[i]); } } Hope that helps!
Sep 11 2005
parent clayasaurus <clayasaurus gmail.com> writes:
Jarrett Billingsley wrote:
 "clayasaurus" <clayasaurus gmail.com> wrote in message 
 news:dg1io2$2mg2$1 digitaldaemon.com...
 
Can I sort the key order in an associative array?

It doesn't work with this example below...

The AA.keys property only returns an array that is a representation of the keys in the AA; thus, sorting the array returned by AA.keys does not affect the original AA. Since the AA is laid out in a nonlinear format, it wouldn't really be possible to sort the key-value pairs. What you can do, however, is get the keys array, sort it, and loop through it, accessing the values in the AA by using the keys array. Like so: import std.stdio; void main() { int[int] array; array[0] = 3; array[100] = 1; array[4] = 10; int[] keys = array.keys; keys.sort; foreach(int i; keys) { writefln("array[",i,"] = ",array[i]); } } Hope that helps!

Thanks. I think it will help but I'm still going to check out other solutions. ~ Clay
Sep 11 2005
prev sibling parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"clayasaurus" <clayasaurus gmail.com> wrote in message 
news:dg1io2$2mg2$1 digitaldaemon.com...
 Can I sort the key order in an associative array?

The MinTL library supports 2 assoc array types with sorting: HashAA and SortedAA. A HashAA is a hashtable with links between the nodes so that foreach traversals happen in a predictable (and sortable) order. A SortedAA is a red-black tree that maintains items in a specific order at all times. I've modified your code below to use HashAA as an example import std.stdio; import mintl.hashaa; int main() { HashAA!(int,int) array; array[0] = 3; array[100] = 1; array[4] = 10; array.sort; // i want the new key order to be 0, 4, 100 foreach(int key; array.keys) { writefln("key(",key,")"); } // prints 0, 100, 4 return 0; }
 It doesn't work with this example below...

 ////////////////////////////////////////////////////////
 //Ex: Trying to sort assoc array key order
 ////////////////////////////////////////////////////////
 import std.stdio;

 int main()
 {


    int[int] array;

    array[0] = 3;
    array[100] = 1;
    array[4] = 10;

    array.keys.sort;

    // i want the new key order to be 0, 4, 100

    foreach(int key; array.keys)
    {
       writefln("key(",key,")");
    }

    // prints 0, 100, 4

    return 0;
 }

 Thanks ahead to anyone who knows the answer.

 ~ Clay 

Sep 11 2005