[flexcoders] Sort object by property name

2007-10-31 Thread Claudia Barnal
Hi,

How can I sort an Object?

For example I have the following object:
{banana:yellow, apple:green, lemon:yellow, cherry:red}

and I need to sort it so that it looks like this:
{apple:green, banana:yellow, cherry:red, lemon:yellow}

I need to assign the colors in a for in loop, for which the order is important.

Thanks,
Claudia


Re: [flexcoders] Sort object by property name

2007-10-31 Thread Edward Yakop
 Hi,
Hi

 How can I sort an Object?

 For example I have the following object:
 {banana:yellow, apple:green, lemon:yellow, cherry:red}

 and I need to sort it so that it looks like this:
 {apple:green, banana:yellow, cherry:red, lemon:yellow}


code
var notSorted:Object= {banana:yellow, apple:green, lemon:yellow,
cherry:red};
var properties:Array = new Array();
for ( var abc:Object in notSorted )
{
  properties.push( abc );
}
properties.sort();

// Now the property names are sorted
for each ( var prop:Object in properties )
{
  trace( prop ); // Do something Or u can do notSorted[ prop ], to
access the value.
}
/code

Regards,
Edward Yakop


Re: [flexcoders] Sort object by property name

2007-10-31 Thread Claudia Barnal
Thank you Edward... just what I needed.

On 10/31/07, Edward Yakop [EMAIL PROTECTED] wrote:

Hi,
 Hi

  How can I sort an Object?
 
  For example I have the following object:
  {banana:yellow, apple:green, lemon:yellow, cherry:red}
 
  and I need to sort it so that it looks like this:
  {apple:green, banana:yellow, cherry:red, lemon:yellow}
 

 code
 var notSorted:Object= {banana:yellow, apple:green, lemon:yellow,
 cherry:red};
 var properties:Array = new Array();
 for ( var abc:Object in notSorted )
 {
 properties.push( abc );
 }
 properties.sort();

 // Now the property names are sorted
 for each ( var prop:Object in properties )
 {
 trace( prop ); // Do something Or u can do notSorted[ prop ], to
 access the value.
 }
 /code

 Regards,
 Edward Yakop