286
Chapter 12: ActionScript Dictionary
Performing a sort that returns an index array doesn’t change the elements in the array:
// Before sorting
// my_array[0].age = 29;
// my_array[1].age = 3;
// my_array[2].age = 35;
// my_array[3].age = 4;
// After a sort that returns an array containing index values
// Note that the original array is unchanged.
// You can then use the returned array to display sorted information
// without modifying the original array.
var indexArray:Array = my_array.sortOn("age", Array.RETURNINDEXEDARRAY);
// my_array[0].age = 29;
// my_array[1].age = 3;
// my_array[2].age = 35;
// my_array[3].age = 4;
Example
This example creates a new array and sorts it according to the fields
name
and
city
: The first sort
uses
name
as the first sort value and
city
as the second. The second sort uses
city
as the first sort
value and
name
as the second.
var rec_array = new Array();
rec_array.push( { name: "john", city: "omaha", zip: 68144 } );
rec_array.push( { name: "john", city: "kansas city", zip: 72345 } );
rec_array.push( { name: "bob", city: "omaha", zip: 94010 } );
for(i=0; i<rec_array.length; i++) {
trace(rec_array[i].name + ", " + rec_array[i].city);
}
// results in
// john, omaha
// john, kansas city
// bob, omaha
rec_array.sortOn( [ "name", "city" ]);
for(i=0; i<rec_array.length; i++) {
trace(rec_array[i].name + ", " + rec_array[i].city);
}
// results in
// bob, omaha
// john, kansas city
// john, omaha
rec_array.sortOn( ["city", "name" ]);
for(i=0; i<rec_array.length; i++) {
trace(rec_array[i].name + ", " + rec_array[i].city);
}
// results in
// john, kansas city
// bob, omaha
// john, omaha
See also
| (bitwise OR)
,
Array.sort()
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...