O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.4. Sorting Recordsets by a Single Column

Problem

You want to sort the records in a recordset, ordering them by the values in a single column.

Solution

Use the RecordSet.sortItemsBy( ) method.

Discussion

If you want to do a simple sort (sorting according to one column) on a recordset, use the sortItemsBy( ) method. This method takes the name of the column on which to sort as a parameter, and it sorts the records within the original recordset (no copy is made). Passing the value “DESC” to the method as a second parameter sorts the records in descending order. Otherwise, the sort order is ascending.

#include "NetServices.as"

rs = new RecordSet(["ID", "NAME"]);
rs.addItem({ID: 24, NAME: "a"});
rs.addItem({ID: 42, NAME: "b"});
rs.addItem({ID: 66, NAME: "c"});
rs.addItem({ID: 93, NAME: "d"});
rs.addItem({ID: 33, NAME: "e"});

// Sort the recordset by ID values in ascending order.
rs.sortItemsBy("ID");

/* Outputs:
   record 0
     ID: 24
     NAME: a
    record 1
     ID: 33
     NAME: e
   record 2
     ID: 42
     NAME: b
   record 3
     ID: 66
     NAME: c
   record 4
     ID: 93
     NAME: d
*/
rs.trace(  );

// Sort the recordset by the NAME column in descending order.
rs.sortItemsBy("NAME", "DESC");

/* Outputs:
   record 0
     ID: 33
     NAME: e
   record 1
     ID: 93
     NAME: d
   record 2
     ID: 66
     NAME: c
   record 3
     ID: 42
     NAME: b
   record 4
     ID: 24
     NAME: a
*/
rs.trace(  );

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required