The ICollectionView is a view of the underlying data source that represents a collection of items. The interface is more complex than that of the IList interface. The following can be achieved with the ICollectionView interface:
Modify the view to show data in a sorted order, or show only a subset of the items using the filtering capabilities without changing the underlying data.
Move through the collection, use bookmarks to save locations in the collection, and add and remove items by accessing the data using a cursor.
Represent remote data that might not initially be available yet, or the dataset that may become available at different times.
Sorting Collections
The Sort class allows you to sort on data in collections. Multiple fields are supported when sorting data, and require that the entries be unique. A custom comparison function can be used to order the sorted output, and the Sort class can also search for item in the collection. If you apply a sort instance to the collection, you are required to call the refresh() method of the collection to apply the changes.
The SortField class is used to specify the fields to use in the sort. You create an instance of the SortField class and add it to the fields array property of the Sort class instance. All the SortField instances of the collection must be unique.
The following example sorts an ArrayCollection in descending order. The primary sort is on the label field of the collection, case-sensitive, and in descending order.
import mx.collections.SortField;
import mx.collections.Sort;
import mx.collections.ArrayCollection;
[Bindable]
public var myCollection:ArrayCollection = new ArrayCollection([{label:
"A"}, {label: "B"}, {label: "C"}, {label: "D"}, {label: "E"}]);
public function sortCollection():void
{
var sort:Sort = new Sort();
sort.fields = [new SortField("label", true, true)];
myCollection.sort = sort;
myCollection.refresh();
}
Filtering Collections
Filtering limits the ICollectionView to a subset of the data provider’s underlying data. You define a function that takes a single Object parameter and returns a Boolean value to specify whether to include the item in the view. The same step applies with sorting as it does with filtering. You must call the refresh() method of the collection when you set the filter function.
The following example uses both filtering and sorting of an ArrayCollection. There are two Button controls to apply the sort and filtering, respectively, and one to reset the view to its original state.
import mx.collections.SortField; public function sortCollection():void public function filterCollection():void private function stateFilterFunc(item:Object):Boolean public function resetCollection():void ]]>
import mx.collections.Sort;
import mx.collections.ArrayCollection;
{
var sort:Sort = new Sort();
sort.fields = [new SortField("label", true, true)];
myCollection.sort = sort;
myCollection.refresh();
}
{
myCollection.filterFunction = stateFilterFunc;
myCollection.refresh();
}
{
return item.label >= “A” && item.label < "N";
}
{
myCollection.filterFunction = null;
myCollection.sort = null;
myCollection.refresh();
}
IViewCursor Interface
The IViewCursor interface defines the functionality to enumerate over a collection view. The ICollectionView interface contains a createCursor() method that returns an IViewCursor object referred to as a cursor. The cursor is used to transverse the items in the view, as well as to access and manipulate data. It is a position indicator that points to a particular item in the collection.
The IViewCursor interface has the following methods and properties to perform the following operations:
Move the cursor backward and forward.
Move it to specific items.
Get the item at a cursor location.
Add, remove, and change items.
Save the cursor as a bookmark so that you can return to it at a later stage.
To use the ICollectionView interface, you must access it by calling the method of a collection instance, as shown in the following code snippet:
public var myArrayCollection:IViewCursor;
public var myCursor:IViewCursor;
public function getCursor():void
{
myCursor = myArrayCollection.createCursor();
}
The moveNext() and movePrevious() methods are used to move the cursor forward and backward by one item at a time. To check whether you have reached the bounds of the collection, use the beforeFirst and afterLast properties.
To search within the collection, you can use the findAny(), findFirst(), and findLast() methods that match the parameter, but you must apply a Sort to the ICollectionView instance. It is important to note that if you do not need to find the first or last occurrence of an item in the collection, the findAny() method is more efficient than the findFirst() and findLast() methods.
To find the last occurrence of an item in the collection, you must apply a Sort, as the following example shows:
var sort:Sort = new Sort();
// the null parameter on the SortField constructor specifies a collection of simple
objects (String, Boolean or numeric)
sort.fields = [new SortField(null, true)];
myCollection.sort = sort;
myCollection.refresh();
myCursor.firstLast(“NY”);
You can search for a complex object using the findLast() method on multiple sort fields. However, you cannot skip fields in any of the find methods. If the object has three fields, you can specify the combinations 1 or 1, 2 or 1, 2, 3 as a parameter, but not 1, 3. The following code will find the last object with a label field “NY” and a data field “New York”:
myCursor.firstLast({label: “NY”, data: “New York”});
The seek() method moves the cursor to a location at an offset from the specified bookmark. You can move to the first and last element in the collection, or to the last bookmark you saved.
The IViewCursor interface has methods and properties to access and manipulate data. The current property is a reference to the item at the current cursor location. The insert() method adds an item before the cursor’s current position, and the remove() method removes the current item and returns a reference to it. When inserting an item using the insert() method of the IViewCursor interface and the collection view is sorted, the inserted item will move to the sorted location and not the cursor location.

