In this section:
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.
The syntax for the STACK SORT command is
STACK SORT stackname BY [HIGHEST] column [BY [HIGHEST] column ...] [;]
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.
If specified, sorts the stack in descending order. If not specified, the stack is sorted in ascending order.
Is a stack column name. 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 Command Syntax.
The following sorts the stack Emp using the values in the stack column Emp_ID:
STACK SORT Emp BY Emp_ID;
The following sorts the stack Emp 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;
You can sort data in a stack without considering case by implementing the using CASE_INSENSITIVE parameter.
To sort data in a stack and ignore case-sensitivity:
STACK SORT stackname BY [HIGHEST] column [BY [HIGHEST] column ...] [using CASE_INSENSITIVE] [;]
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.