table sort (selected) rows

Prev Next

Function Names

table sort rows, table sort selected rows

Description

This function sorts the row according to sorting criteria defined, i.e. columns and applicable sorting options. The function table sort selected rows will only sort the applicable rows, but leave all remaining rows untouched, i.e. in their original locations.

Call as: procedure

Restrictions

Indirect parameter passing is disabled
This function provides a table context for partial table specifications with table name and row number for selected function parameters

Parameter count

2-4

Parameters

No.TypeDescription
1.
input
string Name of existing table

Opt. 2.
code
expression
:string
Expression to select rows

Applicable to table sort selected rows only:
Specify the conditions or rules to select the rows. See expressions to select rows.
The table context for partial table specifications is available for referencing just the columns easily. Example: [Year]>=2022.
Attention: Comparison operators = and <> (instead of == and !=) may hijack the next function parameters for its own use as additional comparison operands, e.g. a[]=1,3,5. As long this comparison is not made in the last function parameter, then put parentheses around them, e.g. ... , (a[]=1,3,5), ....

2 / 3.
input
table columns Columns considered for sorting order

If more than 1 column is specified, then the first column has the highest sorting priority, with additional columns considered if the prior comparison resulted in equal contents, e.g. sorting by last name followed by first name.

See table columns as function parameters for general ruling for this parameter.
Specific rules apply for this function:

  • Any number of header names and column numbers may be specified

Opt. 3 / 4
input
string Sorting option

See sorting options for available values.

Default value: alphabetic

Examples

       table initialize( animals,
               { { Animal, Leg Count },
                 { ant, 6 }, { mouse, 4 }, { spider, 4 }, { octopus, 8 }, { bird, 2 },
                 { tick, 8 }, { orang utan, 2 or 4 }, { human, 2 }, { lobster, 8 }, { snake, 0 },
                 { cat, 4 }, { kangaroo, 2 or 4 }, { spider, 6 }, { fly, 6 }, { worm, 0 } }  );

       echo("Sort animals.  Spiders are sorted by leg count.");
       table copy table ( animals, a );
       table sort rows( a, { Animal, Leg Count }, { alphabetic, numeric down } );
       table list( a );

       echo("Sort animals with 4 legs");
       table rearrange selected rows( animals, ([Leg Count]='*4*'), top );
       table sort selected rows( animals, ([Leg Count]='*4*'), Animal );
       table list ( animals );

Output

Sort animals.  Spiders are sorted by leg count.
    0 : Animal     | Leg Count
    1 : ant        | 6        
    2 : bird       | 2        
    3 : cat        | 4        
    4 : fly        | 6        
    5 : human      | 2        
    6 : kangaroo   | 2 or 4   
    7 : lobster    | 8        
    8 : mouse      | 4        
    9 : octopus    | 8        
   10 : orang utan | 2 or 4   
   11 : snake      | 0        
   12 : spider     | 6        
   13 : spider     | 4        
   14 : tick       | 8        
   15 : worm       | 0        

Sort animals with 4 legs
    0 : Animal     | Leg Count
    1 : cat        | 4        
    2 : kangaroo   | 2 or 4   
    3 : mouse      | 4        
    4 : orang utan | 2 or 4   
    5 : spider     | 4        
    6 : ant        | 6        
    7 : octopus    | 8        
    8 : bird       | 2        
    9 : tick       | 8        
   10 : human      | 2        
   11 : lobster    | 8        
   12 : snake      | 0        
   13 : spider     | 6        
   14 : fly        | 6        
   15 : worm       | 0        

Try it yourself: Open LIB_Function_table_sort_rows.b4p in B4P_Examples.zip. Decompress before use.

See also

table rank rows
table rank selected rows
table sort columns