07-05-2018, 06:08 PM
A simple program for sorting a matrix by your choice of column, in either ascending or descending order.
Usage
Arguments:
y: Matrix
x: Column number (1-based)
Flag 1: Clear = Ascending Order, Set = Descending Order
Put the arguments on the stack and XEQ "SORT". The sorted matrix will be left on the stack in x. Attempting to sort a 1-row matrix will exit cleanly without doing anything.
EDIT: A nicer version that uses the built-in (hidden) "[MIN]" and "[MAX]" functions to do selection sort. When keying this in, note that the brackets are part of the function name and must be included! I've written that function as XEQ "[MIN]" for clarity, but you'll only see [MIN] after entering the command. Thanks Joe.
Usage
Arguments:
y: Matrix
x: Column number (1-based)
Flag 1: Clear = Ascending Order, Set = Descending Order
Put the arguments on the stack and XEQ "SORT". The sorted matrix will be left on the stack in x. Attempting to sort a 1-row matrix will exit cleanly without doing anything.
EDIT: A nicer version that uses the built-in (hidden) "[MIN]" and "[MAX]" functions to do selection sort. When keying this in, note that the brackets are part of the function name and must be included! I've written that function as XEQ "[MIN]" for clarity, but you'll only see [MIN] after entering the command. Thanks Joe.
Code:
LBL "SORT"
RDown
STO "_D"
RDown
STO "_Z"
RDown
STO "_T"
RDown
INDEX "_D"
1
X<>Y
STOIJ
LBL 00
RCLIJ
X<>Y
FC? 01
XEQ "[MIN]"
FS? 01
XEQ "[MAX]"
RDown
R<>R
I+
FC? 76
GTO 00
RCL "_T"
ENTER
ENTER
RCL "_Z"
RCL "_D"
CLV "_M"
CLV "_R"
CLV "_D"
CLV "_Z"
CLV "_T"
END