user2932566
user2932566

Reputation: 97

Sorted Object/Map in AS3 Flex?

Is it possible to sort an object or have some sort of order in an object in Actionscript 3 Flex? I have an object which is created like so:

private var data:Object = new Object();

I then go on to put some keys and values into it:

this.data.someKeyOne = 0;
this.data.someKeyTwo = 1337;
this.data.someKeyThree = 123;

After that I loop through the object:

for (var dataPart:String in this.data) {
    trace("Key: " + dataPart = " Value:" + this.data["dataPart"]);
}

But my issue is that the data will not be displayed in the order I initialized them in. This is frustrating as it is required to be in the order initialized.

Upvotes: 3

Views: 1950

Answers (2)

akmozo
akmozo

Reputation: 9839

As @helloflash said in his answer about loop through an object, and according to Adobe's definition of associative array (which is an instance of the Object class) :

An associative array, sometimes called a hash or map, uses keys instead of a numeric index to organize stored values. Each key in an associative array is a unique string that is used to access a stored value. An associative array is an instance of the Object class, which means that each key corresponds to a property name. Associative arrays are unordered collections of key and value pairs. Your code should not expect the keys of an associative array to be in a specific order.

The behavior that you got is normal and you can not get ordered keys in your case (using an Object like that).

If you don't need keys, you can do as @helloflash said in his answer, using a simple Array or Vector. Otherwise you can use an Array of objects, like this :

var obj:Object, i:int, s:String;

var data_array:Array = [
        {key: 'someKeyOne', value: 0},
        {key: 'someKeyTwo', value: 1337},
        {key: 'someKeyThree', value: 123}
    ]
for(i = 0; i < data_array.length; i++){
    obj = data_array[i];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for (s in data_array) {
    obj = data_array[s];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for each (obj in data_array){
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}

// all these 3 loops give :     
//  Key : someKeyOne, Value = 0
//  Key : someKeyTwo, Value = 1337
//  Key : someKeyThree, Value = 123

Or you can use Object and then sort it into an Array like this :

var data_object:Object = new Object();
    data_object.someKeyOne   = {index:0, value: 0};
    data_object.someKeyTwo   = {index:1, value: 1337};
    data_object.someKeyThree = {index:2, value: 123};

var tmp_array:Array = sort_obj(data_object);

for(i = 0; i < tmp_array.length; i++){
    obj = tmp_array[i];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for (obj in tmp_array) {
    obj = tmp_array[obj];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for each (obj in tmp_array){
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}

function sort_obj(obj:Object):Array {
    var a:Array = [];
    for (var key:String in obj) {
        a.push({key: key, index: obj[key].index, value: obj[key].value});
    }
    a.sortOn('index', Array.NUMERIC);
    return a;
}

// all these 3 loops give :     
//  Key : someKeyOne, Value = 0
//  Key : someKeyTwo, Value = 1337
//  Key : someKeyThree, Value = 123

Hope all that can help you.

Upvotes: 6

helloflash
helloflash

Reputation: 2457

Adobe help about looping:

The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order)

Conclusion

If you want your keys to appear in order, you must use an Array (or a Vector):

var myArray:Array = [0, 1337, 123];

for (var i:String in myArray) {
    trace(myArray[i]);
}
// output: 0, 1337, 123

Upvotes: 1

Related Questions