# 3.7.5.55 Sort

LabTalk Object Type:

Utility

The sort object controls the sorting sequence, order, and range within a worksheet.

## Properties:

Property Access Description
numeric

The column number of the beginning range for sorting. If this number is zero, then all columns in the worksheet are sorted.

numeric

The column number of the end range for sorting. This is ignored if c1 is zero.

sort.cNamen$Read/write string An enumerated string specifying the sort sequence (primary, secondary, etc.) and order (ascending or descending). The syntax is: sort.cnamen$ = {A|D}: ColumnName; The space between : and ColumnName is required. n is 1, 2, etc. for the primary, secondary, etc. sort sequence. A for ascending order and D for descending order. ColumnName is the name of the column, which should be one column between c1 and c2

numeric

Missing values: 0 = treat missing values as the smallest values within a column, 1 = treat missing values as the largest values within a column.

numeric

The row number of the beginning range for sorting.

numeric

The row number of the end range for sorting.

sort.wksName$Read/write string The name of the worksheet to be sorted. ## Methods: Method Description sort.rank(Dataset1, Dataset2[, n]) Dataset1 is the dataset to be ranked. Dataset2 is the dataset that will hold the ranking results. If n = 0, rank in ascending order. If n = 1, rank in descending order. If n is not included, rank in ascending order. When this method is executed, Origin internally sorts Dataset1 in ascending (or descending, n = 1) order. It then finds the average rank (or row number) for each of the duplicate values. This average value is then appended to Dataset2 in each of the corresponding duplicate cells. sort.toggle(index) Changes the sort order for the sequence specified by index. Index is equivalent to n in the sort.cnamen$ property, so that index = 1 for the primary sort sequence, index = 2 for the secondary sort sequence, etc.

sort.wks()

Executes the sort.

## Examples:

### Sort Columns, Specify Primary and Secondary Sort Columns

This script sorts all columns in the Data1 worksheet from row 3 to row 11, using column B as the primary sort column (descending) and column A as the secondary sort column (ascending).

sort.wksname$= [Book1]Data1!; // For the worksheet Data1 in the Book1 sort.c1 = 0; // all columns sort.r1 = 3; // from row 3 to sort.r2 = 11; // row 11, sort.cname1$ = D: B;
// using column B as the primary, sort in descending order
sort.cname2\$ = A: A;
// and column A as the secondary, sort in ascending order
sort.wks();  // execute sort

### Changing Sort Sequence

This script changes the secondary sort sequence from ascending to descending.

sort.toggle(2);  // Changes the sort order for the
// secondary sorting sequence
// here, changes column A from ascending to descending.
sort.wks();  // execute sort

### Ranking

This script ranks col(1) in descending order and places the ranking results in col(2).

range r1=col(1);
range r2=col(2);
sort.rank(r1,r2,1);
doc -uw;