System.IComparable.CompareTo(T)

Here are the examples of the csharp api System.IComparable.CompareTo(T) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

587 Examples 7

19 Source : Heap.cs
with MIT License
from atalantus

private void SortDown(T item)
        {
            while (true)
            {
                var childIndexLeft = item.HeapIndex * 2 + 1;
                var childIndexRight = item.HeapIndex * 2 + 2;

                if (childIndexLeft < Count)
                {
                    var swapIndex = childIndexLeft;

                    if (childIndexRight < Count)
                        if (_items[childIndexLeft].CompareTo(_items[childIndexRight]) < 0)
                            swapIndex = childIndexRight;

                    if (item.CompareTo(_items[swapIndex]) < 0)
                        Swap(item, _items[swapIndex]);
                    else
                        return;
                }
                else
                {
                    return;
                }
            }
        }

19 Source : UAPUtils.cs
with MIT License
from atenfyr

public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
        {
            if (val.CompareTo(min) < 0) return min;
            else if (val.CompareTo(max) > 0) return max;
            else return val;
        }

19 Source : Heap.cs
with MIT License
from atalantus

private void SortUp(T item)
        {
            var parentIndex = (item.HeapIndex - 1) / 2;

            while (true)
            {
                var parenreplacedem = _items[parentIndex];
                if (item.CompareTo(parenreplacedem) > 0)
                    Swap(item, parenreplacedem);
                else
                    break;

                parentIndex = (item.HeapIndex - 1) / 2;
            }
        }

19 Source : CheckExtensions.cs
with Apache License 2.0
from atata-framework

internal static T CheckGreaterOrEqual<T>(this T value, string argumentName, T checkValue, string errorMessage = null)
            where T : struct, IComparable<T>
        {
            if (value.CompareTo(checkValue) < 0)
                throw new ArgumentOutOfRangeException(argumentName, value, ConcatMessage($"Invalid {typeof(T).FullName} value: {value}. Should be greater or equal to: {checkValue}.", errorMessage));

            return value;
        }

19 Source : CheckExtensions.cs
with Apache License 2.0
from atata-framework

internal static T CheckLessOrEqual<T>(this T value, string argumentName, T checkValue, string errorMessage = null)
            where T : struct, IComparable<T>
        {
            if (value.CompareTo(checkValue) > 0)
                throw new ArgumentOutOfRangeException(argumentName, value, ConcatMessage($"Invalid {typeof(T).FullName} value: {value}. Should be less or equal to: {checkValue}.", errorMessage));

            return value;
        }

19 Source : DescendingComparer.cs
with GNU General Public License v3.0
from atomex-me

public int Compare(T x, T y) => y.CompareTo(x);

19 Source : Utility.cs
with GNU Lesser General Public License v2.1
from axiom3d

public static T Clamp<T>(T value, T max, T min) where T : System.IComparable<T>
        {
            var result = value;
            if (value.CompareTo(max) > 0)
            {
                result = max;
            }
            if (value.CompareTo(min) < 0)
            {
                result = min;
            }
            return result;
        }

19 Source : Utility.cs
with GNU Lesser General Public License v2.1
from axiom3d

public static T Max<T>(T value, T max) where T : System.IComparable<T>
        {
            var result = value;
            if (value.CompareTo(max) < 0)
            {
                result = max;
            }
            return result;
        }

19 Source : Utility.cs
with GNU Lesser General Public License v2.1
from axiom3d

public static T Min<T>(T value, T min) where T : System.IComparable<T>
        {
            var result = value;
            if (value.CompareTo(min) > 0)
            {
                result = min;
            }
            return result;
        }

19 Source : RestrictedBinaryHeap.cs
with MIT License
from azyobuzin

public void Push(T value)
            {
                var nodeIndex = this._count++;

                while (nodeIndex > 0)
                {
                    var parentIndex = (nodeIndex - 1) / 2;

                    if (value.CompareTo(this._array[parentIndex]) <= 0) break;

                    this._array[nodeIndex] = this._array[parentIndex];
                    nodeIndex = parentIndex;
                }

                this._array[nodeIndex] = value;
            }

19 Source : RestrictedBinaryHeap.cs
with MIT License
from azyobuzin

public void ReplaceHead(T value)
            {
                var nodeIndex = 0;

                while (true)
                {
                    var childIndex = 2 * nodeIndex + 1;

                    if (childIndex >= this._count) break;

                    if (childIndex + 1 < this._count && this._array[childIndex + 1].CompareTo(this._array[childIndex]) > 0)
                        childIndex++;

                    if (value.CompareTo(this._array[childIndex]) >= 0) break;

                    this._array[nodeIndex] = this._array[childIndex];
                    nodeIndex = childIndex;
                }

                this._array[nodeIndex] = value;
            }

19 Source : ExtensionMethods.cs
with GNU General Public License v3.0
from BardMusicPlayer

public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T> {
            if(val == null)
                throw new ArgumentNullException(nameof(val), "is null.");
            if(min == null)
                throw new ArgumentNullException(nameof(min), "is null.");
            if(max == null)
                throw new ArgumentNullException(nameof(max), "is null.");
            //If min <= max, clamp
            if(min.CompareTo(max) <= 0)
                return val.CompareTo(min) < 0 ? min : val.CompareTo(max) > 0 ? max : val;
            //If min > max, clamp on swapped min and max
            return val.CompareTo(max) < 0 ? max : val.CompareTo(min) > 0 ? min : val;
        }

19 Source : ValueChange.cs
with GNU Lesser General Public License v2.1
from BattletechModders

internal T? Change(T originalValue)
        {
            if (!FromIsMin && !Equals(originalValue, From))
            {
                return null;
            }

            var newValue = Add(originalValue, By);

            if (NewMin.HasValue)
            {
                var min = NewMin.Value;
                if (newValue.CompareTo(min) < 0)
                {
                    return min;
                }
            }

            return newValue;
        }

19 Source : NumberRange.cs
with MIT License
from bitcake

protected static T GetMin( T a, T b )
		{
			return a.CompareTo( b ) <= 0 ? a : b;
		}

19 Source : NumberRange.cs
with MIT License
from bitcake

protected static T GetMax( T a, T b )
		{
			return a.CompareTo( b ) >= 0 ? a : b;
		}

19 Source : UltEventUtils.cs
with MIT License
from BLUDRAG

public static void StableInsertionSort<T>(IList<T> list) where T : IComparable<T>
        {
            StableInsertionSort(list, (a, b) => a.CompareTo(b));
        }

19 Source : SearchSeriesComparer.cs
with GNU General Public License v3.0
from bonarr

public int Compare<T>(Series x, Series y, Func<Series,T> keySelector)
            where T : IComparable<T>
        {
            var keyX = keySelector(x);
            var keyY = keySelector(y);

            return keyX.CompareTo(keyY);
        }

19 Source : Extensions.cs
with MIT License
from BotBuilderCommunity

public static T Min<T>(T one, T two)
            where T : IComparable<T>
        {
            var compare = one.CompareTo(two);
            return compare < 0 ? one : two;
        }

19 Source : Range.cs
with MIT License
from BotBuilderCommunity

public int CompareTo(Range<T> other)
        {
            if (this.After.CompareTo(other.Start) < 0)
            {
                return -1;
            }
            else if (other.After.CompareTo(this.Start) > 0)
            {
                return +1;
            }
            else
            {
                return 0;
            }
        }

19 Source : Extensions.cs
with MIT License
from BotBuilderCommunity

public static IEnumerable<Range<T>> SortedMerge<T>(this IEnumerable<Range<T>> oneItems, IEnumerable<Range<T>> twoItems)
            where T : IEquatable<T>, IComparable<T>
        {
            using (var one = oneItems.GetEnumerator())
            using (var two = twoItems.GetEnumerator())
            {
                T oneIndex = default(T);
                T twoIndex = default(T);

                bool oneMore = one.MoveNext();
                bool twoMore = two.MoveNext();

                if (oneMore)
                {
                    oneIndex = one.Current.Start;
                }

                if (twoMore)
                {
                    twoIndex = two.Current.Start;
                }

                if (oneMore && twoMore)
                {
                    while (true)
                    {
                        var compare = oneIndex.CompareTo(twoIndex);
                        if (compare < 0)
                        {
                            var after = Min(one.Current.After, twoIndex);
                            oneMore = Advance(one, ref oneIndex, after);
                            if (!oneMore)
                            {
                                break;
                            }
                        }
                        else if (compare == 0)
                        {
                            var after = Min(one.Current.After, two.Current.After);
                            yield return new Range<T>(oneIndex, after);
                            oneMore = Advance(one, ref oneIndex, after);
                            twoMore = Advance(two, ref twoIndex, after);
                            if (!(oneMore && twoMore))
                            {
                                break;
                            }
                        }
                        else if (compare > 0)
                        {
                            var after = Min(two.Current.After, oneIndex);
                            twoMore = Advance(two, ref twoIndex, after);
                            if (!twoMore)
                            {
                                break;
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
            }
        }

19 Source : Extensions.cs
with MIT License
from BotBuilderCommunity

private static bool Advance<T>(IEnumerator<Range<T>> enumerator, ref T index, T after)
            where T : IEquatable<T>, IComparable<T>
        {
            index = after;
            var compare = index.CompareTo(enumerator.Current.After);
            if (compare < 0)
            {
                return true;
            }
            else if (compare == 0)
            {
                bool more = enumerator.MoveNext();
                if (more)
                {
                    index = enumerator.Current.Start;
                }

                return more;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }

19 Source : PriorityQueue.cs
with GNU General Public License v3.0
from BramDC3

public int CompareTo(IndexedItem other)
            {
                var c = Value.CompareTo(other.Value);
                if (c == 0)
                    c = Id.CompareTo(other.Id);
                return c;
            }

19 Source : BinarySearch.cs
with MIT License
from Bunny83

public static int BinarySearch<T>(this List<T> aList, T aValue) where T : System.IComparable<T>
    {
        if (aList == null)
            return int.MinValue;
        int min = 0;
        int max = aList.Count - 1;
        if (aList[min].CompareTo(aList[max]) > 0)
        {   // highest element comes first

            if (aValue.CompareTo(aList[min]) > 0)
                return -1;
            if (aValue.CompareTo(aList[max]) < 0)
                return -aList.Count;
            while (min < max-1)
            {
                int mid = (max + min) / 2;
                int p = aValue.CompareTo(aList[mid]);
                if (p == 0)
                    return mid;
                if (p > 0)
                    max = mid;
                else
                    min = mid;
            }
            return -min - 1;
        }
        else
        {   // smallest element comes first

            if (aValue.CompareTo(aList[min]) > 0)
                return -1;
            if (aValue.CompareTo(aList[max]) < 0)
                return -aList.Count;
            while (min < max - 1)
            {
                int mid = (max + min) / 2;
                int p = aValue.CompareTo(aList[mid]);
                if (p == 0)
                    return mid;
                if (p > 0)
                    min = mid;
                else
                    max = mid;
            }
            return -min - 1;
        }
    }

19 Source : ComparableExtensions.cs
with MIT License
from CacoCode

[Description("���ֵ�Ƿ������Сֵ�����ֵ֮��")]
        public static bool IsBetween<T>(this T value, T minInclusiveValue, T maxInclusiveValue) where T : IComparable<T>
        {
            return value.CompareTo(minInclusiveValue) >= 0 && value.CompareTo(maxInclusiveValue) <= 0;
        }

19 Source : ComparableExtensions.cs
with MIT License
from CalciumFramework

public static T Clamp<T>(this T value, T minimum, T maximum) 
			where T : IComparable<T>
		{
			if (value.CompareTo(minimum) < 0)
			{
				return minimum;
			}

			if (value.CompareTo(maximum) > 0)
			{
				return maximum;
			}

			return value;
		}

19 Source : ValueSequence.OrderBy.cs
with MIT License
from CareBoo

public int Compare(T x, T y)
        {
            return x.CompareTo(y);
        }

19 Source : Interval.cs
with MIT License
from chillersanim

public bool Contains(T element)
        {
            return Min.CompareTo(element) <= 0 && Max.CompareTo(element) >= 0;
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

[NotNull]
        [PublicAPI]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static T Max<T>([NotNull] T a, [NotNull] T b)
            where T : IComparable<T>
        {
            return a.CompareTo(b) > 0 ? a : b;
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

[NotNull]
        [PublicAPI]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static T Max<T>([NotNull] T a, [NotNull] T b, [NotNull] T c)
            where T : IComparable<T>
        {
            return a.CompareTo(b) > 0 ? (a.CompareTo(c) > 0 ? a : c) : (b.CompareTo(c) > 0 ? b : c);
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

[NotNull]
        [PublicAPI]
        public static T Max<T>([NotNull] params T[] values)
            where T : IComparable<T>
        {
            if ((values == null) || (values.Length == 0))
            {
                throw new ArgumentException("Values cannot be null or empty.", nameof(values));
            }

            // Preliminary null check for all items.
            if (!typeof(T).IsValueType)
            {
                for (var i = 0; i < values.Length; i++)
                {
                    if (Equals(values[i], null))
                    {
                        throw new ArgumentNullException(nameof(values), "All items must not be null.");
                    }
                }
            }

            // ReSharper disable replacedignNullToNotNullAttribute, PossibleNullReferenceException

            if (values.Length == 1)
            {
                return values[0];
            }

            var max = values[0];

            for (var i = 1; i < values.Length; i++)
            {
                if (values[i].CompareTo(max) > 0)
                {
                    max = values[i];
                }
            }

            return max;

            // ReSharper restore replacedignNullToNotNullAttribute, PossibleNullReferenceException
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

[NotNull]
        [PublicAPI]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static T Min<T>([NotNull] T a, [NotNull] T b)
            where T : IComparable<T>
        {
            return a.CompareTo(b) < 0 ? a : b;
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

[NotNull]
        [PublicAPI]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static T Min<T>([NotNull] T a, [NotNull] T b, [NotNull] T c)
            where T : IComparable<T>
        {
            return a.CompareTo(b) < 0 ? (a.CompareTo(c) < 0 ? a : c) : (b.CompareTo(c) < 0 ? b : c);
        }

19 Source : MeshBuilder.cs
with MIT License
from chillersanim

private bool HasDuplicates<T>(IList<T> sortedItems) where T : IComparable<T>
        {
            for(var i = 1; i < sortedItems.Count; i++)
            {
                if (sortedItems[i].CompareTo(sortedItems[i - 1]) == 0)
                {
                    return true;
                }
            }

            return false;
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

[NotNull]
        [PublicAPI]
        public static T Min<T>([NotNull] params T[] values)
            where T : IComparable<T>
        {
            if ((values == null) || (values.Length == 0))
            {
                throw new ArgumentException("Values cannot be null or empty.", nameof(values));
            }

            // Preliminary null check for all items.
            if (!typeof(T).IsValueType)
            {
                for (var i = 0; i < values.Length; i++)
                {
                    if (Equals(values[i], null))
                    {
                        throw new ArgumentNullException(nameof(values), "All items must not be null.");
                    }
                }
            }

            // ReSharper disable replacedignNullToNotNullAttribute, PossibleNullReferenceException

            if (values.Length == 1)
            {
                return values[0];
            }

            var min = values[0].CompareTo(values[1]) < 0 ? values[0] : values[1];

            for (var i = 2; i < values.Length; i++)
            {
                if (values[i].CompareTo(min) < 0)
                {
                    min = values[i];
                }
            }

            return min;

            // ReSharper restore replacedignNullToNotNullAttribute, PossibleNullReferenceException
        }

19 Source : CommonUtil.cs
with MIT License
from chillersanim

public static void Sort<T>(ref T t0, ref T t1) where T : IComparable<T>
        {
            if (t0.CompareTo(t1) > 0)
            {
                var tmp = t0;
                t0 = t1;
                t1 = tmp;
            }
        }

19 Source : KnownSeriesRandom.cs
with MIT License
from Chris3606

private static T returnIfRange<T>(T minValue, T maxValue, List<T> series, ref int seriesIndex) where T : IComparable<T>
		{
			T value = returnValueFrom(series, ref seriesIndex);

			if (minValue.CompareTo(value) < 0)
				throw new ArgumentException("Value returned is less than minimum value.");

			if (maxValue.CompareTo(value) >= 0)
				throw new ArgumentException("Value returned is greater than/equal to maximum value.");

			return value;
		}

19 Source : KnownSeriesRandom.cs
with MIT License
from Chris3606

private static T returnIfRangeInclusive<T>(T minValue, T maxValue, List<T> series, ref int seriesIndex) where T : IComparable<T>
		{
			T value = returnValueFrom(series, ref seriesIndex);

			if (minValue.CompareTo(value) < 0)
				throw new ArgumentException("Value returned is less than minimum value.");

			if (maxValue.CompareTo(value) > 0)
				throw new ArgumentException("Value returned is greater than/equal to maximum value.");

			return value;
		}

19 Source : TimelineInspectorUtility.cs
with MIT License
from chstetco

internal static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
        {
            if (val.CompareTo(min) < 0) return min;
            if (val.CompareTo(max) > 0) return max;
            return val;
        }

19 Source : AbpComparableExtensions.cs
with GNU Lesser General Public License v3.0
from cnAbp

public static bool IsBetween<T>(this T value, T minInclusiveValue, T maxInclusiveValue) where T : IComparable<T>
        {
            return value.CompareTo(minInclusiveValue) >= 0 && value.CompareTo(maxInclusiveValue) <= 0;
        }

19 Source : ValueRange.cs
with MIT License
from cocosip

public bool Contains(T value)
        {
            return MinValue.CompareTo(value) <= 0 && MaxValue.CompareTo(value) >= 0;
        }

19 Source : ValueRange.cs
with MIT License
from cocosip

public void Join(T value)
        {
            if (MinValue.CompareTo(value) > 0)
            {
                MinValue = value;
            }
            if (MaxValue.CompareTo(value) < 0)
            {
                MaxValue = value;
            }
        }

19 Source : ValidationUtils.cs
with MIT License
from CragonGame

public static void ArgumentIsPositive<T>(T value, string parameterName) where T : struct, IComparable<T>
    {
      if (value.CompareTo(default(T)) != 1)
        throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Positive number required.");
    }

19 Source : Guard.cs
with Apache License 2.0
from crazyants

[DebuggerStepThrough]
        public static void IsPositive<T>(T arg, string argName, string message = IsPositiveMessage) where T : struct, IComparable<T>
        {
            if (arg.CompareTo(default(T)) < 1)
                throw ErrorHelper.ArgumentOutOfRange(argName, message.FormatInvariant(argName));
        }

19 Source : Guard.cs
with Apache License 2.0
from crazyants

[DebuggerStepThrough]
        public static void InRange<T>(T arg, T min, T max, string argName) where T : struct, IComparable<T>
        {
            if (arg.CompareTo(min) < 0 || arg.CompareTo(max) > 0)
                throw ErrorHelper.ArgumentOutOfRange(argName, "The argument '{0}' must be between '{1}' and '{2}'.", argName, min, max);
        }

19 Source : Guard.cs
with Apache License 2.0
from crazyants

[DebuggerStepThrough]
        public static void NotNegative<T>(T arg, string argName, string message = NotNegativeMessage) where T : struct, IComparable<T>
        {
            if (arg.CompareTo(default(T)) < 0)
                throw ErrorHelper.ArgumentOutOfRange(argName, message.FormatInvariant(argName, arg));
        }

19 Source : Guard.cs
with Apache License 2.0
from crazyants

[DebuggerStepThrough]
        public static void NotZero<T>(T arg, string argName) where T : struct, IComparable<T>
        {
            if (arg.CompareTo(default(T)) == 0)
                throw ErrorHelper.ArgumentOutOfRange(argName, "Argument '{0}' must be greater or less than zero. Value: '{1}'.", argName, arg);
        }

19 Source : ValueRandom.cs
with Mozilla Public License 2.0
from CreateAndFake

public T Next<T>(T min, T max) where T : struct, IComparable, IComparable<T>, IEquatable<T>
        {
            if (!Supports<T>())
            {
                throw new NotSupportedException($"Type '{typeof(T).Name}' not supported.");
            }
            else if (min.Equals(max))
            {
                return min;
            }
            else if (min.CompareTo(max) >= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(max), max,
                    $"Value must be greater than given min: '{min}'.");
            }
            else if (typeof(T) == typeof(bool))
            {
                return default;
            }
            else
            {
                try
                {
                    return CalcNext(min, max);
                }
                catch (ArithmeticException)
                {
                    return StumbleNext(min, max);
                }
            }
        }

19 Source : ValueRandom.cs
with Mozilla Public License 2.0
from CreateAndFake

private T CalcNext<T>(T min, T max) where T : struct, IComparable, IComparable<T>, IEquatable<T>
        {
            dynamic percent;

            // Creates a number in the range of 0.0 to 1.0.
            if (typeof(T) != typeof(decimal))
            {
                percent = (double)Next<uint>() / uint.MaxValue;
            }
            else
            {
                percent = (decimal)Next<uint>() / uint.MaxValue;
            }

            checked
            {
                T value = (T)(percent * ((dynamic)max - min) + min);

                // Prevent any issues steming from imprecision.
                return (value.CompareTo(max) < 0) ? value : min;
            }
        }

19 Source : Maybe.cs
with MIT License
from crookookoo

public int CompareTo(Maybe<T> other) {
      if (hasValue != other.hasValue) {
        return hasValue ? 1 : -1;
      } else if (hasValue) {
        IComparable<T> ct = _t as IComparable<T>;
        if (ct != null) {
          return ct.CompareTo(other._t);
        } else {
          IComparable c = _t as IComparable;
          if (c != null) {
            return c.CompareTo(other._t);
          } else {
            return 0;
          }
        }
      } else {
        return 0;
      }
    }

19 Source : InsertionSort.cs
with MIT License
from cschen1205

public static void Sort<T>(T[] a) where T : IComparable<T>
        {
            Sort(a, 0, a.Length-1, (a1, a2) => a1.CompareTo(a2));
        }

See More Examples