STACK SORT

In this section:

How to:

The STACK SORT command enables you to sort the contents of a stack in ascending or descending order based on the values in one or more columns.

Syntax: How to Use the STACK SORT Command

The syntax for the STACK SORT command is

STACK SORT stackname BY [HIGHEST] column [BY [HIGHEST] column ...] [;]

where:

stackname

Specifies the stack to be sorted. The stack name cannot be subscripted with a row range in order to sort only part of the stack.

HIGHEST

If specified, sorts the stack in descending order. If not specified, the stack is sorted in ascending order.

column

Is a stack column name or a computed field value. At least one column name must be specified. The column must exist in the specified stack.

;

Terminates the command. Although the semicolon is optional, it is recommended that you include it to allow for flexible syntax and better processing. For more information about the benefits of including the semicolon, see Terminating a Command's Syntax.

Example: Using STACK SORT

The following sorts the Emp stack using the values in the stack column Emp_ID:

STACK SORT Emp BY Emp_ID;

The following sorts the Emp stack so that the employees with the highest Salary are placed first in the stack:

STACK SORT Emp BY HIGHEST Salary;

The next example sorts the stack by Department. Within Department, the rows are ordered by highest Salary:

STACK SORT Emp BY Department BY HIGHEST Salary;

Sorting Data With the Using CASE_INSENSITIVE Parameter

How to:

You can sort data in a stack without considering case by using the CASE_INSENSITIVE parameter.

Syntax: How to Sort Data in a Stack With the Using CASE_INSENSITIVE Parameter

To sort data in a stack and ignore case-sensitivity:

  1. Add the using CASE_INSENSITIVE tablename parameter after the BY fields:
    STACK SORT stackname BY [HIGHEST] column [BY [HIGHEST] column ...] 
    [using CASE_INSENSITIVE] [;]
  2. Import the MNTUWS function library.
  3. Add the following command to the Top case of the Maintain Data procedure:
    perform prep_CASE_INSENSITIVE

    For example:

    MAINTAIN
    MODULE IMPORT (mntuws)
    $$Declarations
    CASE Top
    PERFORM prep_CASE_INSENSITIVE;
    COMPUTE STK(1).NAME/A10="GEORGE";
    STK(2).NAME="Bernard";
    STK(3).NAME="Shaw";
    .
    .
    .
    STACK SORT STK BY NAME using CASE_INSENSITIVE;
    .
    .
    .

    Note: For a back-end procedure without a Top case, the perform prep_CASE_INSENSITIVE command is not required.

    When using MNTCON MATCH_CASE ON, the case of the code must be the same as it appears above.