Rowset Manual Sort

The Rowset Sort method allows you to sort in either ascending or descending values based upon one or more fields. However, occasionally there is a need to sort in a non-linear way. For example, if a field includes the values "First", "Second", "Third", and "Fourth", the sort order is obvious to us, but not to the Sort method.

One route to do this type of sorting is to add an additional field. Assign that field values that will sort correctly. Each row with the value "First" gets a 1, each row with the value "Second" gets a 2... Then call the sort method and pass the new field in.

Why not build your own custom sort method? We can do so by leveraging the fact that PeopleTools drops rows that are not visible to the bottom of the rowset. It's a trick that generally trips everyone up at least once. It's also why when you are hiding rows that you need to go from the bottom back to the top.

Here's some psuedo-code that shows how you might build a custom sort method:

method Sort(&data as rowset, &order as array of string, &recordName as string, &fieldName as string)

    /* start by making all rows not visible */
    &data.HideAllRows();

    /* loop through data, looking for ordering values */
    local integer &i, &j;
local field &f;

    for &i = 1 to &order.len
        local string &orderValue = &order[&i];
       
        for &j = 1 to &data.ActiveRowCount
       
            /* does this row have the sort value? */
&f = &data(&j).GetRecord(@("Record." | &recordName)).GetField(@("Field." | &fieldName));
            if &f.Value = &orderValue Then
                &data(&j).Visible = True;
            end-if;
        end-for;
    end-for;

    /* in case any values were missing, they'll still be hidden */
    &data.ShowAllRows();
   
End-method;

You might assume from the use of the Visible property that this only works for buffer rowsets. However, it turns out that PeopleTools drops hidden rows to the bottom even on stand-alone rowsets. So this works for those, too.



Post a Comment:
Comments are closed for this entry.