这是Studio for .NET 公司C1FlexGrid控件的Sort方法的说明,有用过的来讨论下
Object.Sort(IComparer compare)
的用法。
Sorts the entire grid or a range of rows based on the contents of one or more columns.
Syntax
Object.Sort(SortFlags flags, int col)
Object.Sort(SortFlags flags, int col1, int col2)
Object.Sort(SortFlags flags, CellRange range)
Object.Sort(int index, int count, IComparer compare)
Object.Sort(IComparer compare)
Parameter
Description
SortFlags flags
One or more values from the SortFlags enumeration that specify the type of sorting (e.g. ascending, descending, case-sensitive, etc).
int col
The column that contains the data to be sorted.
int col1, col2
A range of columns to sort.
CellRange range
A range of cells to sort. If a range is specified, the grid will sort only rows that are contained in the range.
IComparer compare
An object that implements the IComparer interface to compare Row objects. This can be used to provide custom sorting.
Remarks
The Sort method allows you to sort specific columns or cell ranges in ascending or descending order. The flags parameter specifies the sorting order and a few additional options, described below:
Flag
Effect
Ascending
Sort rows in ascending order.
Descending
Sort rows in descending order.
AsDisplayed
Sort comparing the displayed value. If this flag is set, the sorting is based on the formatted string that is displayed to the user. If it is not set, the sorting is based on the underlying data value.
IgnoreCase
Use case-insensitive string comparison.
UseColSort
Use the value stored in the Sort property of each column in the range. This allows you to sort some columns in ascending order and other in descending order.
When you sort multiple columns, the same sorting options are applied to each column, starting from the leftmost column in the range and proceeding to the right.
The grid uses a stable sorting algorithm. This means that the sorting keeps the relative order of records when the sorting key is the same. For example, if you sort a list of files by name, then by extension, file names will still be sorted within each extension group.
To sort multiple columns using a different sorting order for each, you can either call the Sort method multiple times or set each column's Sort property and call the Sort method and include the UseColSort flag in the flags parameter.
To implement custom sorts, you should provide an object that implements the IComparer interface. This simple interface has a single method called Compare that takes two objects as arguments (in this case, they will be Row objects) and returns -1, 0, or +1. For more details, see the documentation for the System.Collections.IComparer interface.
Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim row1, row2 As C1.Win.C1FlexGrid.Row
Dim ind1, ind2 As Integer
Dim val1, val2 As Object
row1 = a
row2 = b
ind1 = row1.DataIndex
ind2 = row2.DataIndex
Dim a1, b1 As Decimal
Dim result As Integer
Try
a1 = val1
b1 = val2
If a1 < b1 Then
result = -1
ElseIf a1 = b1 Then
result = 0
Else
result = 1
End If
Catch
result = -1
End Try