RecordSet.sort( ) Method | Flash 6 |
sorts a recordset |
A custom function that facilitates the sort of the recordset by comparing records.
The sort( ) method sorts a RecordSet object in place using a user-specified sort function. The compare function should compare two records (which are passed as two arguments to the function) and return a positive number if the first record is greater than the second record, a negative number if the second record is greater, and 0 otherwise.
The following code demonstrates the sort( ) method on a RecordSet object, sorting by last name and then first name using a custom sortByFirstAndLast( ) function:
#include "RecordSet.as" var myRecordset_rs = new RecordSet(["First", "Last", "Email"]); myRecordset_rs.addItem({First:"Tom", Last:"Muck"}); myRecordset_rs.addItem({First:"Jack", Last:"Splat"}); myRecordset_rs.addItem({First:"Bob", Last:"Splat"}); myRecordset_rs.addItem({First:"Biff", Last:"Splat"}); function sortByFirstAndLast(rec1, rec2) { if (rec1.Last < rec2.Last) return -1; if (rec1.Last > rec2.Last) return 1; if (rec1.First < rec2.First) return -1; if (rec1.First > rec2.First) return 1; return 0; } myRecordset_rs.sort(sortByFirstAndLast); for (var i=0; i < myRecordset_rs.getLength( ); i++) trace(myRecordset_rs.getItemAt(i).Last + ", " + myRecordset_rs.getItemAt(i).First);
In this case, the records are sorted by last name and first name. If we want to sort by only one field, we can use the sortItemsBy( ) method, which is about 10 times faster. After running the sort, the items are now in this order:
RecordSet.filter( ), RecordSet.sortItemsBy( )