本主题演示使用ITableSort接口的工作流程。
1.Implement a custom comparison method
ITableSort tableSort = new TableSortClass();
tableSort.Compare = new StreetNumberSort();
tableSort.Table = table;
tableSort.Fields = "StreetNum";
public class StreetNumberSort: ITableSortCallBack
{
public int Compare(object value1, object value2, int fieldIndex, int
fieldSortIndex)
{
// Seperate the numeric and non-numeric components of each value.
int value1Number = - 1;
int value2Number = - 1;
String value1Suffix = null;
String value2Suffix = null;
SeperateValues(value1.ToString(), out value1Number, out value1Suffix);
SeperateValues(value2.ToString(), out value2Number, out value2Suffix);
// Compare the numeric components of the street numbers.
if (value1Number != value2Number)
{
// If value1 is less than value2, return -1; otherwise, return 1.
if (value1Number < value2Number)
{
return - 1;
}
else
{
return 1;
}
}
// The numeric values are equal. Compare the suffixes.
int compareResult = String.Compare(value1Suffix, value2Suffix);
if (compareResult < 0)
{
return - 1;
}
else if (compareResult == 0)
{
return 0;
}
else
{
return 1;
}
}
private void SeperateValues(String streetNumber, out int number, out String
suffix)
{
// Step through the street number to the end or until a null character is reached.
StringBuilder numberBuilder = new StringBuilder();
StringBuilder suffixBuilder = new StringBuilder();
for (int i = 0; i < streetNumber.Length && streetNumber[i] != '\0'; i++)
{
Char currentChar = streetNumber[i];
if (currentChar >= '0' && currentChar <= '9')
{
numberBuilder.Append(currentChar);
}
else
{
suffixBuilder.Append(currentChar);
}
}
// Set the outbound parameters.
Int32.TryParse(numberBuilder.ToString(), out number);
suffix = suffixBuilder.ToString();
}
}
2.Understand the workflow to sort tables and selection sets
// Expected result. Counties sorted by descending state and ascending county name.
public void SortCursor(ITable table, ICursor cursor)
{
// Construct the sort and set required fields.
ITableSort tableSort = new TableSortClass();
tableSort.Table = table;
tableSort.Fields = "State_Name, Name";
// Set the cursor.
tableSort.Cursor = cursor;
// Refine the sorting parameters.
tableSort.set_Ascending("State_Name", false);
tableSort.set_Ascending("Name", true);
tableSort.set_CaseSensitive("State_Name", true);
tableSort.set_CaseSensitive("Name", true);
// Perform the sort and get results as a cursor.
tableSort.Sort(null);
ICursor resultCursor = tableSort.Rows;
// Display the results.
int stateNameIndex = resultCursor.Fields.FindField("State_Name");
int countyNameIndex = resultCursor.Fields.FindField("Name");
int popIndex = resultCursor.Fields.FindField("POP");
IRow row = null;
while ((row = resultCursor.NextRow()) != null)
{
Console.WriteLine("{0}, {1}, {2}", row.get_Value(stateNameIndex),
row.get_Value(countyNameIndex), row.get_Value(popIndex));
}
}
// Expected result. Return the record for the third largest county
// or get a list of ordered IDs largest to smallest population.
public void SortSelection(ITable table, ISelectionSet selSet)
{
// Construct the sort and set required fields.
ITableSort tableSort = new TableSortClass();
tableSort.Fields = "POP";
tableSort.SelectionSet = selSet;
// Refine the sorting parameters, sort, and get the ID enumeration.
tableSort.set_Ascending("POP", false);
tableSort.Sort(null);
IEnumIDs enumIDs = tableSort.IDs;
// Get the third feature (for example, what is the third largest county?).
int id = tableSort.get_IDByIndex(2); //Zero based index.
IRow row = table.GetRow(id);
// Get field indexes for efficient reuse.
int stateNameIndex = row.Fields.FindField("State_Name");
int countyNameIndex = row.Fields.FindField("Name");
int popIndex = row.Fields.FindField("POP");
// Display the results.
Console.WriteLine("{0}, {1}, {2}", row.get_Value(stateNameIndex), row.get_Value
(countyNameIndex), row.get_Value(popIndex));
// Or iterate through all of the results...
while ((id = enumIDs.Next()) != - 1)
// -1 is returned after the last valid ID has been reached.
{
row = table.GetRow(id);
Console.WriteLine("{0}, {1}, {2}", row.get_Value(stateNameIndex),
row.get_Value(countyNameIndex), row.get_Value(popIndex));
}
}