本文整理汇总了C#中IComparer类的典型用法代码示例。如果您正苦于以下问题:C# IComparer类的具体用法?C# IComparer怎么用?C# IComparer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IComparer类属于命名空间,在下文中一共展示了IComparer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Sort
/// <summary>
/// Rearranges the array in ascending order, using the natural order.
/// </summary>
/// <param name="a">a the array to be sorted</param>
public static void Sort(IComparable[] a, IComparer c = null)
{
//make sure we have the comparer passed by argument or create default
IComparer comparer = c ?? Comparer<object>.Default;
int N = a.Length;
int h = 1;
//3*x +1 -> 1, 4, 13, 40, 121, 364, ..
while (h < N / 3)
{
h = 3 * h + 1;
}
while (h >= 1)
{
//h- sort array
for (int i = h; i < N; i++)
{
for (int j = i; j >= h && less(comparer, a[j], a[j - h]); j -= h)
{
exch(a, j, j - h);
}
}
Debug.Assert(isHsorted(a, comparer, h));
h = h / 3;
}
Debug.Assert(isSorted(a, comparer));
}
示例2: Sort
public void Sort(
int index,
int count,
IComparer comparer)
{
theList.Sort(index, count, comparer);
}
示例3: AddSort
public override void AddSort(object expr, IComparer comparer)
{
// sort makes sense only when we are dealing with a query that
// returns a nodeset.
Query evalExpr;
string query = expr as string;
if (query != null)
{
evalExpr = new QueryBuilder().Build(query, out _needContext); // this will throw if expr is invalid
}
else
{
CompiledXpathExpr xpathExpr = expr as CompiledXpathExpr;
if (xpathExpr != null)
{
evalExpr = xpathExpr.QueryTree;
}
else
{
throw XPathException.Create(SR.Xp_BadQueryObject);
}
}
SortQuery sortQuery = _query as SortQuery;
if (sortQuery == null)
{
_query = sortQuery = new SortQuery(_query);
}
sortQuery.AddSort(evalExpr, comparer);
}
示例4: ListViewColumnSorter
public ListViewColumnSorter(IComparer comparer)
{
SortColumn = 0;
SortOrder = SortOrder.Ascending;
mComparer = comparer;
}
示例5: LastWriteTimeComparator
// Methods
public LastWriteTimeComparator(bool bAscending, IComparer subComparer)
{
this._IsAscending = true;
this._InnerComparer = null;
this.IsAscending = bAscending;
this.InnerComparer = subComparer;
}
示例6: TreeWithPointProvider
public void TreeWithPointProvider(CustomPoint[] point, CustomPoint[] resultPoint, IComparer<CustomPoint> comparer)
{
var tree = new BinaryTree<CustomPoint>(point, comparer);
var pointCompare = new PointComparer();
var enumeratorBook = tree.Preorder().ToArray();
CollectionAssert.AreEqual(enumeratorBook, resultPoint, pointCompare);
}
示例7: create
public static PersistentTreeSet create(IComparer comp, ISeq init)
{
PersistentTreeSet ret = new PersistentTreeSet(null, new PersistentTreeMap(null, comp));
for (ISeq s = init; s != null; s = s.next())
ret = (PersistentTreeSet)ret.cons(s.first());
return ret;
}
示例8: SortedCollection
//==========================================================================================
// Constructors
//==========================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="SortedCollection"/> class.
/// </summary>
/// <param name="comparer">An <see cref="IComparer"/> to use for the sorting.</param>
protected SortedCollection(IComparer comparer)
: base(new ArrayList())
{
Tracer.VerifyNonNullArgument(comparer, "comparer");
this.comparer = comparer;
this.list = (ArrayList)this.InnerCollection;
}
示例9: StockMessageFactory
public StockMessageFactory(IStockRepository repoA, IStockRepository repoB, IComparer<Stock> comparer, char userInput)
{
_repoA = repoA;
_repoB = repoB;
_comparer = comparer;
_userInput = userInput;
}
示例10: 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();
}
}
示例11: SorterBase
/// <summary>
/// Initializes a new instance of the SorterBase class with the specified
/// TableModel, Column index, IComparer and SortOrder
/// </summary>
/// <param name="tableModel">The TableModel that contains the data to be sorted</param>
/// <param name="column">The index of the Column to be sorted</param>
/// <param name="comparer">The IComparer used to sort the Column's Cells</param>
/// <param name="sortOrder">Specifies how the Column is to be sorted</param>
public SorterBase(TableModel tableModel, int column, IComparer comparer, SortOrder sortOrder)
{
this.tableModel = tableModel;
this.column = column;
this.comparer = comparer;
this.sortOrder = sortOrder;
}
示例12: GEPEnvironment
/// <summary>
///
/// </summary>
/// <param name="identifier"></param>
/// <param name="state"></param>
/// <param name="metricsEvaluator"></param>
/// <param name="endCriteriaEvaluator"></param>
/// <param name="getCallableGenes">Gets the IGEPGenes that can be called by the given IGEPGene.</param>
public GEPEnvironment(IEnvironmentIdentifier identifier, IEnvironmentState state,
IMetricsEvaluator metricsEvaluator, IEndCriteriaEvaluator endCriteriaEvaluator,
IComparer<IOrganism> organismValueComparer,
ICallableGenesProvider callableGenesProvider)
: base(identifier, state, metricsEvaluator, endCriteriaEvaluator, callableGenesProvider: callableGenesProvider, organismValueComparer: organismValueComparer)
{
}
示例13: SortHelper
// Helper method for sorting an ArrayList. If the comparer is a SortFieldComparer,
// use the cached-value approach to avoid excessive reflection. For other
// comparers, sort the usual way
internal static void SortHelper(ArrayList al, IComparer comparer)
{
SortFieldComparer sfc = comparer as SortFieldComparer;
if (sfc == null)
{
// sort the usual way
al.Sort(comparer);
}
else
{
// Sort with cached values.
// Step 1. Copy the items into a list augmented with slots for
// the cached values.
int n = al.Count;
int nFields = sfc._fields.Length;
CachedValueItem[] list = new CachedValueItem[n];
for (int i=0; i<n; ++i)
{
list[i].Initialize(al[i], nFields);
}
// Step 2. Sort the augmented list. The SortFieldComparer will
// fill in the slots as necessary to perform its comparisons.
Array.Sort(list, sfc);
// Step 3. Copy the items back into the original list, now in
// sorted order
for (int i=0; i<n; ++i)
{
al[i] = list[i].OriginalItem;
}
}
}
示例14: AreCollectionsEqual
private static bool AreCollectionsEqual(ICollection expected, ICollection actual, IComparer comparer, ref string reason)
{
Assert.CheckParameterNotNull(comparer, "Assert.AreCollectionsEqual", "comparer", string.Empty, new object[0]);
if (!object.ReferenceEquals(expected, actual))
{
if ((expected == null) || (actual == null))
{
return false;
}
if (expected.Count != actual.Count)
{
reason = (string)TestingResources.NumberOfElementsDiff;
return false;
}
IEnumerator enumerator = expected.GetEnumerator();
IEnumerator enumerator2 = actual.GetEnumerator();
for (int i = 0; enumerator.MoveNext() && enumerator2.MoveNext(); i++)
{
if (0 != comparer.Compare(enumerator.Current, enumerator2.Current))
{
reason = (string)TestingResources.ElementsAtIndexDontMatch(i);
return false;
}
}
reason = (string)TestingResources.BothCollectionsSameElements;
return true;
}
reason = (string)TestingResources.BothCollectionsSameReference(string.Empty);
return true;
}
示例15: SortedList
public SortedList()
{
this.keys = emptyArray;
this.values = emptyArray;
this._size = 0;
this.comparer = new Comparer(CultureInfo.CurrentCulture);
}