Worksheet Sort Data
Version Info
Minimum Origin Version Required: Origin8 SR0
Sort Worksheet By One Column
This example shows how to sort data in ascending or descending order based on the values in the specified column number.
//Before running this code, Worksheet with at least one column must exist in project.
void wks_sort_data_ex()
{
Worksheet wks = Project.ActiveLayer(); //Columns in the active worksheet contain values to be sorted.
if(!wks)
{
printf("Error. Please active worksheet before running!");
return;
}
int wByColNum = 1; //column number to sort
bool bMissingValuesSmall = true; //Missing values are treated as smallest. true is also default value.
int iFromRow = 0; //first row number to sort
int iFromCol = 0; //first column number to sort
int iToRow = -1; //last row number to sort, default -1 sorts to last row
int iToCol = -1; //last column number to sort, default -1 sorts to last column
//Sorts column B in worksheet in descending order
bool bRet = wks.Sort(wByColNum, SORT_DESCENDING, bMissingValuesSmall, iFromRow, iFromCol, iToRow, iToCol);
if (bRet)
out_str("Sort successful!");
else
out_str("Sort error!");
}
Nested Sort Worksheet by Customize Settings
This example shows how to do nested sort in worksheet.
The working mechanism of the following example is same as Nested Sort dialog in Origin panel. To open the dialog, you can
- Highlight the columns you want to sort in worksheet
- Right click, select Sort Worksheet, then select Custom...
//Before running this code, Worksheet with at least one column must exist in project.
void wks_nested_sort_data_ex()
{
Worksheet wks = Project.ActiveLayer();
if(!wks)
{
printf("Error. Please active worksheet before running!");
return;
}
vector<int> vSortByCols = {1, 0}; //column numbers to sort. set Column B as primary sort key
vector<int> vOrder = {SORT_ASCENDING, SORT_DESCENDING}; //set the sort order of each column
bool bMissingValuesSmall = true; //Missing values are treated as smallest. true is also default value.
int iFromRow = 0; //first row number to sort
int iFromCol = 0; //first column number to sort
int iToRow = -1; //last row number to sort, default -1 sorts to last row
int iToCol = -1; //last column number to sort, default -1 sorts to last column
bool bRet = wks.Sort(vSortByCols, vOrder, bMissingValuesSmall, iFromRow, iFromCol, iToRow, iToCol);
if (bRet)
out_str("Sort successful!");
else
out_str("Sort error!");
}
|