Get Distinct ArrayCollection Values

This Utility gives you Distinct values from your source array collection. It iterates through the Source ArrayCollection and gets the item Object for each loop and checks to see if the property value of the item is present in the new Collection. If its not present then it add’s the item to the new Collection, else it ignore’s the item and iterates the next item in the list. This is done by getItemIndex property of the ArrayCollection.

  public static function getDisticntArrayCollection(sourceCollection:ArrayCollection):ArrayCollection{
      var distinctValuesCollection:ArrayCollection = new ArrayCollection();
      for each(var item:* in sourceCollection){
          var propertyValue:Object = item.VALUE;
          if(distinctValuesCollection.getItemIndex(propertyValue) == -1)
             distinctValuesCollection.addItem(item);
       }
    return distinctValuesCollection;
  }
Advertisements

One thought on “Get Distinct ArrayCollection Values

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s