table rank (selected) rows

Prev Next

Function Names

table rank rows, table rank selected rows

Description

This function ranks the rows according to ransorting criteria defined, i.e. columns and applicable sorting options. Ranking is a meaningful alternative to identify the ordering of the data without phyically rearranging the rows. Specific rules are provided for ranking multiple entries which are equal.

Call as: procedure or function

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 rank 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 column Destination column for rank numbers

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

  • Only one header name or column numbers may be specified
  • If the specified header does not exist, then an additional column with that header will be created.

Opt. 3 / 4.
input
table columns Columns considered for ranking order

If more than 1 column is specified, then the first column has the highest ranking priority, with additional columns considered if the prior comparison resulted in equal contents, e.g. ranking 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

Default value: alphabetic
Opt. 4 / 5
input
string Ranking option

See sorting options for available values.

Default value: alphabetic
Opt. 5 / 6
input
numeral Ranking scheme for equal values

One of following:

123 Unique ranking. Same values are ranked by their row number location in the table (rows above are ranked higher)
213 Unique ranking. Same values are ranked by their row number location in the table (rows below are ranked higher)
112 Same values get the same higher ranking (2 gold medals for same score followed by silver)
113 Same values get the same higher ranking (2 gold medals for same score followed by bronze)
113 Same values get the same higher ranking (2 sliver medals for same score followed by bronze)

Default value: 123
Opt. 6 / 7
input
numeral Rank starting number

Normally, ranking begins with 1. In some cases, it is useful to apply cascaded rankings on different rows on the same table, and you want to continue with the next lower ranking number in the 2nd or further ranking rounds. See code example below.

Default value: 1

Return value

TypeDescription
numeral Next further rank number

Useful for casecaded ranking (provide as last parameter in the next call)

Examples

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

       echo("Ranked all animals with more than 4 legs from Z-A, then those with 2 legs from A-Z (cascaded ranking)");
       a[] = table rank selected rows( animals, [Leg Count]>4 , Rank, Animal, alphabetic down, 112 );
                 table rank selected rows( animals, [Leg Count]<=2, Rank, Animal, alphabetic, 112, a[] );
       table list ( animals );


       table initialize( names, { Name, Benita, Allen, Dominique, Andy, Benita, BENITA, Dominique, Michele } );

       echo("Different ranking schemes on names using alphabetic ordering, ignoring case, start with rank 0:");
       table process ( names, for all ( { 123, 213, 112, 113, 223 }, scheme[] )
               {
                       table rank rows( names, str(scheme[]), Name, alphabetic ignore case, scheme[], 0 );
               } );
       table list ( names );

Output

Ranked all animals with more than 4 legs from Z-A, then those with 2 legs from A-Z (cascaded ranking)
    0 : Animal  | Leg Count | Rank
    1 : ant     | 6         | 6   
    2 : mouse   | 4         |     
    3 : spider  | 8         | 2   
    4 : octopus | 8         | 3   
    5 : bird    | 2         | 8   
    6 : tick    | 8         | 1   
    7 : human   | 2         | 9   
    8 : ape     | 4         |     
    9 : lobster | 8         | 4   
   10 : snake   | 0         | 10  
   11 : cat     | 4         |     
   12 : ape     | 2         | 7   
   13 : spider  | 6         | 2   
   14 : fly     | 6         | 5   

Different ranking schemes on names using alphabetic ordering, ignoring case, start with rank 0:
    0 : Name      | 123 | 213 | 112 | 113 | 223
    1 : Benita    | 2   | 4   | 2   | 2   | 4  
    2 : Allen     | 0   | 0   | 0   | 0   | 0  
    3 : Dominique | 5   | 6   | 3   | 5   | 6  
    4 : Andy      | 1   | 1   | 1   | 1   | 1  
    5 : Benita    | 3   | 3   | 2   | 2   | 4  
    6 : BENITA    | 4   | 2   | 2   | 2   | 4  
    7 : Dominique | 6   | 5   | 3   | 5   | 6  
    8 : Michele   | 7   | 7   | 4   | 7   | 7  

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

See also

table sort rows
table sort selected rows