Another common task related to fetching data is sorting fetch results in memory. Given an array of objects to filter and a sort ordering that specifies how to sort the array, you can use static methods in EOSortOrdering to sort enterprise objects in memory. The qualifier you use to sort objects in memory is the same type of qualifier you use when fetching data.
For example, to sort an array of enterprise objects in ascending order based on a sellingPrice property, you can use the code in “Listing 6-1.”
Listing 5-1 Sort fetch results in memory
NSArray sortedObjects = EOSortOrdering.sortedArrayUsingKeyOrderArray(objectsToSort, new |
NSArray(new Object[] {EOSortOrdering.sortOrderingWithKey("sellingPrice", |
EOSortOrdering.CompareAscending)})); |
“Listing 6-1” illustrates the four objects required for sorting: an array of enterprise objects to sort (objectsToSort), a String representing the property of the enterprise object to sort on (sellingPrice), an NSSelector object representing how to sort the array (EOSortOrdering.CompareAscending), and an EOSortOrdering object that is made up of the sellingPrice String and the EOSortOrdering.CompareAscending NSSelector. You then invoke EOSortOrdering.sortedArrayUsingKeyOrderArray and pass in the array of objects to sort and the EOSortOrdering object. That method returns an array that is sorted with the specified criteria.
Last updated: 2007-07-11