Monday, August 30, 2010

Working with Arrays in AS3: Part 7

Today we continue learning how to sort elements in an Array.

The previous tutorial described how to use the sort() method. However, it is used on Arrays that has string or number typed elements. With the sortOn() method, you can sort any type objects. You're required to enter one parameter for sortOn() - that is a string parameter of the property's name which the sorting will be based on. For example, we have 3 objects containing information about customers, such as first name, second name and age. We can sort the list by first name, second name or age:

var customers:Array = new Array();
customers.push({firstName: "John", secondName: "Anderson", age: 28});
customers.push({firstName: "Alex", secondName: "Thompson", age: 22});
customers.push({firstName: "Bob", secondName: "Jackson", age: 30});

customers.sortOn("age");

for (var i:int=0; i<customers.length;  i++){
 trace(customers[i].firstName + "  " + customers[i].secondName + "  " + customers[i].age);
 }

It is also possible to sort by more than one properties. If we need to do this, specify an array of the properties as the parameter for sortOn. In this example, the customers are sorted first by name, and then by age:

var customers:Array = new Array();
customers.push({firstName: "John", secondName: "Anderson", age: 28});
customers.push({firstName: "Alex", secondName: "Thompson", age: 22});
customers.push({firstName: "Bob", secondName: "Jackson", age: 30});
customers.push({firstName: "John", secondName: "Nickelson", age: 29});
customers.push({firstName: "Alex", secondName: "Johnson", age: 21});
customers.push({firstName: "Bob", secondName: "Green", age: 29});

customers.sortOn(["firstName", "age"]);

for (var i:int=0; i<customers.length;  i++){
 trace(customers[i].firstName + "  " + customers[i].secondName + "  " + customers[i].age);
 }

Output:

Alex Johnson 21
Alex Thompson 22
Bob Green 29
Bob Jackson 30
John Anderson 28
John Nickelson 29

Now, this might be a little confusing at first. How can something be sorted by more than one properties?! Well, look: we have 2 Bobs aged 29 and 30, and they are both in the Array. If we sort the Array by names, both Bobs will be together. If we now sort them by age, Bob Green will go up, because he is younger. Now if we change Bob Green's age to 31:

customers.push({firstName: "Bob", secondName: "Green", age: 31});

And then sort, then we will get this output:

Alex Johnson 21
Alex Thompson 22
Bob Jackson 30
Bob Green 31
John Anderson 28
John Nickelson 29

See the difference?

We can also sort first by age, and then by names.

var customers:Array = new Array();
customers.push({firstName: "John", secondName: "Anderson", age: 28});
customers.push({firstName: "Alex", secondName: "Thompson", age: 22});
customers.push({firstName: "Bob", secondName: "Jackson", age: 30});
customers.push({firstName: "John", secondName: "Nickelson", age: 29});
customers.push({firstName: "Alex", secondName: "Johnson", age: 21});
customers.push({firstName: "Bob", secondName: "Green", age: 29});

customers.sortOn(["age", "firstName"]);

for (var i:int=0; i<customers.length;  i++){
 trace(customers[i].firstName + "  " + customers[i].secondName + "  " + customers[i].age);
 }

Output:

Alex Johnson 21
Alex Thompson 22
John Anderson 28
Bob Green 29
John Nickelson 29
Bob Jackson 30

Just like with sort(), we can add modificators to sortOn() after the first parameter, such as: Array.CASEINSENSETIVITY, Array.NUMERIC, Array.DESCENDING and so on.

customers.sortOn("firstName", Array.DESCENDING);

Thanks for reading!

Related:

Working with Arrays in AS3: Part 1
Working with Arrays in AS3: Part 2
Working with Arrays in AS3: Part 3
Working with Arrays in AS3: Part 4
Working with Arrays in AS3: Part 5
Working with Arrays in AS3: Part 6

No comments:

Post a Comment