SpreadsheetGear 2023
Sort Method (IRange)
Example 


SpreadsheetGear Namespace > IRange Interface : Sort Method
Specifies whether to sort rows or columns.
Specifies whether sorting should be case sensitive.
Specifies an array of sort keys.
Sorts the cells represented by this range according to the specified options.
Syntax
'Declaration
 
Sub Sort( _
   ByVal orientation As SortOrientation, _
   ByVal caseSensitive As System.Boolean, _
   ByVal ParamArray keys() As SortKey _
) 
'Usage
 
Dim instance As IRange
Dim orientation As SortOrientation
Dim caseSensitive As System.Boolean
Dim keys() As SortKey
 
instance.Sort(orientation, caseSensitive, keys)
void Sort( 
   SortOrientation orientation,
   System.bool caseSensitive,
   params SortKey[] keys
)

Parameters

orientation
Specifies whether to sort rows or columns.
caseSensitive
Specifies whether sorting should be case sensitive.
keys
Specifies an array of sort keys.
Example
Demonstrate sorting from top to bottom with 2 keys.
/// <SUMMARY>
/// Demonstrate sorting from top to bottom with 2 keys.
/// </SUMMARY>
static void Sort()
{
 // Create a workbook.
 IWorkbook workbook = Factory.GetWorkbook();
 
// Get the first worksheet and name it.
 IWorksheet worksheet = workbook.Worksheets[0];
 worksheet.Name = "Sorted by Sales then Product";
 
// Get a reference to a range of cells.
 SpreadsheetGear.IRange range = worksheet.Cells["A1:B8"];
 
// Load products in no particular order.
 range[0, 0].Formula = "Oregano";
 range[1, 0].Formula = "Marjoram";
 range[2, 0].Formula = "Basil";
 range[3, 0].Formula = "Rosemary";
 range[4, 0].Formula = "Thyme";
 range[5, 0].Formula = "Black Pepper";
 range[6, 0].Formula = "Garlic Powder";
 range[7, 0].Formula = "Chili Powder";
 
// Load random data and format as $ using multiple cell range.
 SpreadsheetGear.IRange body = range[0, 1, 7, 1];
 body.Formula = "=ROUND(RAND() * 5000, -3)";
 body.NumberFormat = "$#,##0_);($#,##0)";
 
// Get rid of formulas...
 body.Value = body.Value;
 
// Set up the first sort key with a key index of one representing the second
 // column (random $ amounts) in the range, descending sort order, and the
 // normal data option.
 SpreadsheetGear.SortKey sortKey1 = new SpreadsheetGear.SortKey(
 1, SpreadsheetGear.SortOrder.Descending, SpreadsheetGear.SortDataOption.Normal);
 
// Set up the second sort key with a key index of zero representing the first
 // column (products) in the range, ascending sort order, and the normal data option.
 //
 // This sort key will be used when the values in the first sort key are equal.
 SpreadsheetGear.SortKey sortKey2 = new SpreadsheetGear.SortKey(
 0, SpreadsheetGear.SortOrder.Ascending, SpreadsheetGear.SortDataOption.Normal);
 
// Sort the range by rows, ignoring case, passing the sort key.
 // NOTE: Any number of sort keys may be passed to the Sort method.
 range.Sort(SpreadsheetGear.SortOrientation.Rows, false, sortKey1, sortKey2);
 
// AutoFit the range.
 range.Columns.AutoFit();
 
// Save the workbook.
 workbook.SaveAs(@"C:\SortedBySalesThenProduct.xlsx", FileFormat.OpenXMLWorkbook);
}
'/ <SUMMARY>
'/ Demonstrate sorting from top to bottom with 2 keys. 
'/ </SUMMARY>
Private Shared Sub Sort()
 ' Create a workbook. 
Dim workbook As IWorkbook = Factory.GetWorkbook()
 
' Get the first worksheet and name it. 
Dim worksheet As IWorksheet = workbook.Worksheets(0)
 worksheet.Name = "Sorted by Sales then Product"
 
' Get a reference to a range of cells. 
Dim range As SpreadsheetGear.IRange = worksheet.Cells("A1:B8")
 
' Load products in no particular order. 
range(0, 0).Formula = "Oregano"
 range(1, 0).Formula = "Marjoram"
 range(2, 0).Formula = "Basil"
 range(3, 0).Formula = "Rosemary"
 range(4, 0).Formula = "Thyme"
 range(5, 0).Formula = "Black Pepper"
 range(6, 0).Formula = "Garlic Powder"
 range(7, 0).Formula = "Chili Powder"
 
' Load random data and format as $ using multiple cell range. 
Dim body As SpreadsheetGear.IRange = range(0, 1, 7, 1)
 body.Formula = "=ROUND(RAND() * 5000, -3)"
 body.NumberFormat = "$#,##0_);($#,##0)"
 
' Get rid of formulas... 
body.Value = body.Value
 
' Set up the first sort key with a key index of one representing the second 
' column (random $ amounts) in the range, descending sort order, and the 
' normal data option. 
Dim sortKey1 As New SpreadsheetGear.SortKey(1, SpreadsheetGear.SortOrder.Descending, SpreadsheetGear.SortDataOption.Normal)
 
' Set up the second sort key with a key index of zero representing the first 
' column (products) in the range, ascending sort order, and the normal data option. 
' 
' This sort key will be used when the values in the first sort key are equal. 
Dim sortKey2 As New SpreadsheetGear.SortKey(0, SpreadsheetGear.SortOrder.Ascending, SpreadsheetGear.SortDataOption.Normal)
 
' Sort the range by rows, ignoring case, passing the sort key. 
' NOTE: Any number of sort keys may be passed to the Sort method. 
range.Sort(SpreadsheetGear.SortOrientation.Rows, False, sortKey1, sortKey2)
 
' AutoFit the range. 
range.Columns.AutoFit()
 
' Save the workbook. 
workbook.SaveAs("C:\SortedBySalesThenProduct.xlsx", FileFormat.OpenXMLWorkbook)
 End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

IRange Interface
IRange Members