SpreadsheetGear
Sort Method
See Also  Example Send Feedback
SpreadsheetGear Namespace > IRange Interface : Sort Method

orientation
Specifies whether to sort rows or columns.
caseSensitive
Specifies whether sorting should be case sensitive.
keys
Specifies an array of sort keys.
Sorts the cells represented by this range according to the specified options.

Syntax

Visual Basic (Declaration) 
Sub Sort( _
   ByVal orientation As SortOrientation, _
   ByVal caseSensitive As Boolean, _
   ByVal ParamArray keys() As SortKey _
) 
C# 
void Sort( 
   SortOrientation orientation,
   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.
C#Copy Code
/// <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);
}
Visual BasicCopy Code
'/ <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

Platforms: x86 and x64 versions of Windows 2000, Windows XP, Windows Vista, Windows 7, Windows Server 2003 (including R2) and Windows Server 2008 (including R2). SpreadsheetGear 2009 requires the Microsoft .NET Framework 2.0 and supports .NET 3.0 and .NET 3.5.

See Also

Copyright © 2003-2009 SpreadsheetGear LLC. All Rights Reserved.Help Powered by Innovasys   
SpreadsheetGear is a registered trademark of SpreadsheetGear LLC.
Microsoft, Microsoft Excel and Visual Studio are trademarks or registered trademarks of Microsoft Corporation.