本文整理汇总了C#中System.Collections.IComparer.Compare方法的典型用法代码示例。如果您正苦于以下问题:C# IComparer.Compare方法的具体用法?C# IComparer.Compare怎么用?C# IComparer.Compare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.IComparer
的用法示例。
在下文中一共展示了IComparer.Compare方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: return
//引入命名空间
using System;
using System.Collections;
public class Example
{
public class ReverserClass : IComparer
{
// Call CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(y, x));
}
}
public static void Main()
{
// Initialize a string array.
string[] words = { "The", "quick", "brown", "fox", "jumps", "over",
"the", "lazy", "dog" };
// Display the array values.
Console.WriteLine("The array initially contains the following values:" );
PrintIndexAndValues(words);
// Sort the array values using the default comparer.
Array.Sort(words);
Console.WriteLine("After sorting with the default comparer:" );
PrintIndexAndValues(words);
// Sort the array values using the reverse case-insensitive comparer.
Array.Sort(words, new ReverserClass());
Console.WriteLine("After sorting with the reverse case-insensitive comparer:");
PrintIndexAndValues(words);
}
public static void PrintIndexAndValues(IEnumerable list)
{
int i = 0;
foreach (var item in list )
Console.WriteLine($" [{i++}]: {item}");
Console.WriteLine();
}
}
输出:
The array initially contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog After sorting with the default comparer: [0]: brown [1]: dog [2]: fox [3]: jumps [4]: lazy [5]: over [6]: quick [7]: the [8]: The After sorting with the reverse case-insensitive comparer: [0]: the [1]: The [2]: quick [3]: over [4]: lazy [5]: jumps [6]: fox [7]: dog [8]: brown