/// <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);
}