views:

30

answers:

1

Suppose I have a list of objects of a certain custom type and I need to find an object given only the value of one of its attributes, how do I do it?

Eg.

  // Where user1 ... usern are objects of some class User
  users = [ user1, user2, user3 ... usern ] 

  // How do I find out the objects that have the "foo" attribute set to "bar"
+1  A: 

You can use the filter() method of the array:

var fooBarUsers:* = users.filter(function (user:User) {
    return user.foo == "bar";
});

To sort based on an attribute, use the sortOn method:

fooBarUsers.sortOn("foo");          // Sorts in-place by default

There are lots of options for the sortOn() method; be sure to read the documentation.

Cameron
Thanks. In case I need to sort based on the attribute, what do I do then?
Yasmin Hanifa
@Yasmin: See my edited answer
Cameron
Got it. Thanks =)
Yasmin Hanifa