本文整理汇总了C#中IComparer.Compare方法的典型用法代码示例。如果您正苦于以下问题:C# IComparer.Compare方法的具体用法?C# IComparer.Compare怎么用?C# IComparer.Compare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IComparer
的用法示例。
在下文中一共展示了IComparer.Compare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsPuttable
/// <summary>
/// Don't overwrite already cached items
/// </summary>
/// <param name="txTimestamp"></param>
/// <param name="newVersion"></param>
/// <param name="comparator"></param>
/// <returns></returns>
public bool IsPuttable(long txTimestamp, object newVersion, IComparer comparator)
{
// we really could refresh the item if it
// is not a lock, but it might be slower
//return freshTimestamp < txTimestamp
return version != null && comparator.Compare(version, newVersion) < 0;
}
示例2: SetOp
private SetOp() { } // disable construction
/// <summary>
/// Computes union of two sorted sequences.
/// </summary>
/// <remarks>
/// <para>Both set1 and set2 must be sorted in ascending order with respect to comparer.</para>
/// <para>Union contains elements present in one or both ranges.</para>
/// <para>Result is written to the output iterator one member at a time</para>
///
/// <para>Union differs from <see cref="Merge">Merge</see> for multisets.</para>
///
/// <para>If k equal elements are present in set1 and m elements equal to those k
/// are present in set2,then k elements from set1 are included in the output,
/// followed by max(m-k, 0) elements from set2. The total of max(k,m) are
/// added to the output. If you'd like to have m+k elements, use Merge function.
/// </para>
/// <para>Complexity: linear on combined number of items in both sequences</para>
/// </remarks>
/// <example>
/// <para>set1 = { "a", "test", "Test", "z" }</para>
/// <para>set2 = { "b", "tEst", "teSt", "TEST", "Z" }</para>
/// <para>comparer is a case-insensitive comparer</para>
/// <para>The following elements will be added to output:
/// {"a", "b", "test", "Test", "TEST", "z" }</para>
/// </example>
public static void Union(IEnumerable set1, IEnumerable set2, IComparer comparer, IOutputIterator output) {
IEnumerator enum1 = set1.GetEnumerator();
IEnumerator enum2 = set2.GetEnumerator();
bool have1 = enum1.MoveNext();
bool have2 = enum2.MoveNext();
while (have1 && have2) {
int compare = comparer.Compare(enum1.Current, enum2.Current);
if (compare < 0) {
output.Add(enum1.Current);
have1 = enum1.MoveNext();
} else if (compare > 0) {
output.Add(enum2.Current);
have2 = enum2.MoveNext();
} else {
output.Add(enum1.Current);
have1 = enum1.MoveNext();
have2 = enum2.MoveNext();
}
}
while (have1) {
output.Add(enum1.Current);
have1 = enum1.MoveNext();
}
while (have2) {
output.Add(enum2.Current);
have2 = enum2.MoveNext();
}
}
示例3: FindIndex
protected virtual int FindIndex(object item, object seed, IComparer comparer, int low, int high)
{
if (comparer != null)
{
ListComparer comparer2 = comparer as ListComparer;
if (comparer2 != null)
{
comparer2.Reset();
}
CollectionViewGroupComparer comparer3 = comparer as CollectionViewGroupComparer;
if (comparer3 != null)
{
comparer3.Reset();
}
int num = low;
while (num < high)
{
CollectionViewGroupInternal internal2 = base.ProtectedItems[num] as CollectionViewGroupInternal;
object y = (internal2 != null) ? internal2.SeedItem : base.ProtectedItems[num];
if ((y != DependencyProperty.UnsetValue) && (comparer.Compare(seed, y) < 0))
{
return num;
}
num++;
}
return num;
}
return high;
}
示例4: SimpleSort
/// <summary>
/// A simple bubble sort for two arrays, limited to some region of the arrays.
/// This is a regular bubble sort, nothing fancy.
/// </summary>
public static void SimpleSort(Array array, Array items, int startingIndex, int length, IComparer comparer)
{
bool finished = false;
while (!finished)
{
bool swapped = false;
for (int g = startingIndex; g < startingIndex + length - 1; g++)
{
Object first = array.GetValue(g);
Object second = array.GetValue(g + 1);
int comparison = comparer.Compare(first, second);
if (comparison == 1)
{
Swap(g, g + 1, array, items);
swapped = true;
first = array.GetValue(g);
second = array.GetValue(g + 1);
}
}
if (!swapped)
{
finished = true;
}
}
}
示例5: Partition
public static int Partition( this int[] arr, int start, int end, int pivot,
IComparer<int> comparer)
{
if ( pivot < start || pivot > end )
throw new IndexOutOfRangeException( "Pivot was out of range" );
if ( end <= start )
return pivot;
int pVal = arr[pivot];
arr.Swap( end, pivot );
int i = start, j = end - 1;
while ( i < j )
{
while ( i < j && comparer.Compare( arr[i], pVal ) <= 0 )
++i;
while ( i < j && comparer.Compare( arr[j], pVal ) > 0 )
--j;
if ( i < j )
arr.Swap( i, j );
}
if ( comparer.Compare( arr[i], pVal ) <= 0 )
++i;
arr[end] = arr[i];
arr[i] = pVal;
return i;
}
示例6: Difference
/// <summary>
/// Computes difference of two sorted sequences.
/// </summary>
/// <remarks>
/// <para>Both set1 and set2 must be sorted in ascending order with respect to comparer.</para>
/// <para>Difference contains elements present in set1, but not in set2.</para>
/// <para>Result is written to the output iterator one member at a time</para>
///
/// <para>For multisets, if set1 contains k equal elements, and set2 contains
/// m elements equal to those k, then max(k-m,0) elements from set1 are
/// included in the output.</para>
/// <para>Complexity: linear on combined number of items in both sequences</para>
/// </remarks>
/// <example>
/// <para>set1 = {"a", "b", "test", "tEst", "z" }</para>
/// <para>set2 = {"a", "TEST", "z", "Z" }</para>
/// <para>comparer = case insensitive comparer</para>
/// <para>output = {"b", "tEst"}</para>
/// </example>
public static void Difference(IEnumerable set1, IEnumerable set2, IComparer comparer, IOutputIterator output)
{
var enum1 = set1.GetEnumerator();
var enum2 = set2.GetEnumerator();
var have1 = enum1.MoveNext();
var have2 = enum2.MoveNext();
while (have1 && have2)
{
var compare = comparer.Compare(enum1.Current, enum2.Current);
if (compare < 0)
{
output.Add(enum1.Current);
have1 = enum1.MoveNext();
}
else if (compare > 0)
{
have2 = enum2.MoveNext();
}
else
{
have1 = enum1.MoveNext();
have2 = enum2.MoveNext();
}
}
while (have1)
{
output.Add(enum1.Current);
have1 = enum1.MoveNext();
}
}
示例7: Compare
private static int Compare(IComparer<Object> comparer, Object[] array, int leftIndex, int rightIndex) {
// item 64
Object left = array[leftIndex];
Object right = array[rightIndex];
// item 56
return comparer.Compare(left, right);
}
示例8: BubbleSort
private static void BubbleSort(int[][] array, IComparer comparer)
{
int length = array.Length;
for (int i = 0; i < length; i++)
for (int j = 0; j < length - i - 1; j++)
if (comparer.Compare(array[j], array[j+1]) > 0)
Swap(ref array[j], ref array[j + 1]);
}
示例9: SortWithInterfaces
public static void SortWithInterfaces(double[][] array, IComparer<double[]> comparer)
{
for (int i = 0; i < array.Length - 1; ++i)
for (int j = 0; j < array.Length - i - 1; ++j)
{
if (comparer.Compare(array[j], array[j + 1]) > 0)
Swap(ref array[j], ref array[j + 1]);
}
}
示例10: Contains
static bool Contains(IList list, object value, IComparer comparer)
{
foreach(object item in list) {
if(0 == comparer.Compare(item, value)) {
return true;
}
}
return false;
}
示例11: SortColumn
private void SortColumn(int idx, IComparer<Object> cmp)
{
for(int i = 1; i < table.Count; i++) {
for(int j = i; j > 0; j--)
{
if (cmp.Compare(table[j][idx], table[j - 1][idx]) > 0)
SwapTable(j, j - 1);
}
}
}
示例12: InsertionSort
///<param name="list">List to sort</param>
///<param name="lbound">Lower bound, smallest index to include in sort</param>
///<param name="ubound">Upper bound, larges index to include in sort</param>
///<param name="comparer">Object to compare collections elements and determine sort order</param>
public static void InsertionSort(IList list, int lbound, int ubound, IComparer comparer)
{
for(int i=lbound + 1; i <= ubound; i++) {
object item = list[i];
if(comparer.Compare(item, list[i-1]) < 0) {
//i is not in sorted order
list.RemoveAt(i);
SortedInsert(item, list, lbound, i-1, comparer);
}
}
}
示例13: Sort
public static void Sort(int[][] array, IComparer<int[]> compareArg)
{
for (int i = 0; i < array.GetLength(0) - 1; i++)
{
for (int j = i + 1; j < array.GetLength(0); j++)
{
if (compareArg.Compare(array[i], array[j]) > 0)
Swap(ref array[i], ref array[j]);
}
}
}
示例14: BubbleSort
private static void BubbleSort(int[][] array, IComparer<int[]> comparer)
{
int n = array.Length;
while (n- 1 > 0)
{
for (int i = 0; i < array.Count() - 1; i++)
if (comparer.Compare(array[i], array[i + 1]) > 0)
Swap(ref array[i], ref array[i+1]);
n--;
}
}
示例15: TestComparisons
/// <summary>
/// Utility method to perform appropriate comparisons with the given comparer. A10 should
/// be less than B15 for all our tests
/// </summary>
static void TestComparisons(IComparer<NameAndNumber> comparer)
{
Assert.AreEqual(0, comparer.Compare(A10, A10));
Assert.AreEqual(0, comparer.Compare(B15, B15));
Assert.AreEqual(0, comparer.Compare(null, null));
Assert.Less(comparer.Compare(null, A10), 0);
Assert.Greater(comparer.Compare(A10, null), 0);
Assert.Less(comparer.Compare(A10, B15), 0);
Assert.Greater(comparer.Compare(B15, A10), 0);
}