NAME

WeBWorK::Utils::SortRecords - utilities for sorting database records.

SYNOPSIS

use WeBWorK::Utils::SortRecords qw/getSortsForClass/;

# Get a list of sorts
my $sorts = getSortsForClass(ref $users[0], $default_sort);

use WeBWorK::Utils::SortRecords qw/sortRecords/;

# Start with a list of records
my @users = $db->getUsers($db->listUsers);

# Sort the records using a preset.
@sortedUsers = sortRecords('lnfn', @users);

# Sort on a database field for the records.
@sortedUsers = sortRecords('section', @users);

DESCRIPTION

This module provides record sorting functions, and a collection of preset sorts for the standard WeBWorK record classes. Sorts are specified by a list of field names. Sorts are performed lexicographically.

FUNCTIONS

getSortsForClass($class, $default_sort)

Given the name of a record class, returns the sort methods available for that class.

The return value is a reference to a list of two element lists. The first element in each list is a a string description of the sort method and the second element is the sort name. The return value is suitable for passing as the second value argument to the Mojolicious select_field tag helper method.

If the $default_sort is provided then that sort will be marked as the default selected sort. Otherwise the first sort will be marked as the default selected sort.

sortRecords($preset_sort, @records)

Given a preset format or a field from a database record class, and a list of records, returns a list of the same records in order according to the sort.

The $preset_sort must be provided. It must either be one of the presets defined above, or the name of a field in the database record class.