icomparable

Sort an ArrayList of Objects in C#

血红的双手。 提交于 2019-12-02 14:24:51
问题 How can I sort an ArrayList of objects? I have implemented the IComparable interface while sorting the ArrayList , but I am not getting the required result. My code sample: public class Sort : IComparable { public string Count { get; set; } public string Url { get; set; } public string Title { get; set; } public int CompareTo(object obj) { Sort objCompare = (Sort)obj; return (this.Count.CompareTo(objCompare.Count)); } } Here I want to sort the ArrayList based on Count . 回答1: try this: public

PowerShell IComparable with subclasses

烈酒焚心 提交于 2019-12-01 22:31:55
Let's assume we have these 3 classes: Class BaseClass : System.IComparable { [int] $Value BaseClass([int] $v) { $this.Value = $v } [int] CompareTo($that) { If (-Not($that -is [BaseClass])) { Throw "Not comparable!!" } return $this.Value - $that.Value } } Class SubClassA : BaseClass { SubClassA([int] $v) : base($v) { } } Class SubClassB : BaseClass { SubClassB([int] $v) : base($v) { } } This implementation works great when we compare instances of BaseClass : $base1 = [BaseClass]::new(1) $base2 = [BaseClass]::new(2) Write-Output ($base1 -lt $base2) # Output: True Write-Output ($base1 -gt $base2)

Internal working of the Sort() and CompareTo() methods

懵懂的女人 提交于 2019-12-01 21:42:42
I've been trying to figure out how the CompareTo() method works internally and I failed. I've searched this site and read some posts, and I think I've seen all there is to see in MSDN about this subject and I just don't seem to get it. An MSDN example: public int CompareTo(object obj) { if (obj == null) { return 1; } Temperature otherTemperature = obj as Temperature; if (otherTemperature != null) { return this.temperatureC.CompareTo(otherTemperature.temperatureC); } else { throw new ArgumentException("the object is not a temperature"); } } This is the MSDN example of the implementation of the

Problem comparing items implementing IComparable

a 夏天 提交于 2019-12-01 05:27:20
I am working on a extension method where it finds the min item by specific selector. Below the code public static T MinBy<T, K>(this IEnumerable<T> src, Func<T, K> selector) where K : struct, IComparable, IConvertible { var min = default(K); T minItem = default(T); foreach (var item in src) { var current = selector(item); if (current < min) { min = current; minItem = item; } } return minItem; } It gives error Error Operator '<' cannot be applied to operands of type 'K' and 'K' . But i have specified the generic constraint K should be Struct and IComparable . I believe all the numeric data type

Problem comparing items implementing IComparable

烈酒焚心 提交于 2019-12-01 04:12:12
问题 I am working on a extension method where it finds the min item by specific selector. Below the code public static T MinBy<T, K>(this IEnumerable<T> src, Func<T, K> selector) where K : struct, IComparable, IConvertible { var min = default(K); T minItem = default(T); foreach (var item in src) { var current = selector(item); if (current < min) { min = current; minItem = item; } } return minItem; } It gives error Error Operator '<' cannot be applied to operands of type 'K' and 'K' . But i have

Modify List.Contains behavior

冷暖自知 提交于 2019-12-01 03:54:33
I have a List<MyObj> with the class MyObj : IComparable . I wrote the method CompareTo in the MyObj class per the IComparable interface, but when I use the List<MyObj>.Contains(myObjInstance) it returns false when it should be true . I'm not sure I'm understanding how I need to proceed to make sure the List uses my custom comparison method when calling then Contains function. Here is my compareTo implementation: #region IComparable Members public int CompareTo(object obj) { MyObj myObj = (MyObj)obj; return String.Compare(this.Symbol, myObj.Symbol, true); } #endregion Note the Symbol property

IComparable and IComparable<T>

徘徊边缘 提交于 2019-11-30 03:13:48
Should I implement both IComparable and the generic IComparable<T> ? Are there any limitations if I only implement one of them? Yes, you should implement both. If you implement one, any code that depends on the other will fail. There is lots of code that uses either IComparable or IComparable<T> but not both, so implementing both ensure your code will work with such code. Oded is right that you should implement both because there are collections and other classes that rely on only one of the implementations. But there is a trick there: IComparable<T> should not throw exceptions, while

Native C# support for checking if an IEnumerable is sorted?

戏子无情 提交于 2019-11-29 11:19:41
Is there any LINQ support for checking if an IEnumerable<T> is sorted? I have an enumerable that I want to verify is sorted in non-descending order, but I can't seem to find native support for it in C#. I've written my own extension method using IComparables<T> : public static bool IsSorted<T>(this IEnumerable<T> collection) where T : IComparable<T> { Contract.Requires(collection != null); using (var enumerator = collection.GetEnumerator()) { if (enumerator.MoveNext()) { var previous = enumerator.Current; while (enumerator.MoveNext()) { var current = enumerator.Current; if (previous.CompareTo

IComparable and IComparable<T>

百般思念 提交于 2019-11-28 23:40:40
问题 Should I implement both IComparable and the generic IComparable<T> ? Are there any limitations if I only implement one of them? 回答1: Yes, you should implement both. If you implement one, any code that depends on the other will fail. There is lots of code that uses either IComparable or IComparable<T> but not both, so implementing both ensure your code will work with such code. 回答2: Oded is right that you should implement both because there are collections and other classes that rely on only

Native C# support for checking if an IEnumerable is sorted?

我与影子孤独终老i 提交于 2019-11-28 04:57:43
问题 Is there any LINQ support for checking if an IEnumerable<T> is sorted? I have an enumerable that I want to verify is sorted in non-descending order, but I can't seem to find native support for it in C#. I've written my own extension method using IComparables<T> : public static bool IsSorted<T>(this IEnumerable<T> collection) where T : IComparable<T> { Contract.Requires(collection != null); using (var enumerator = collection.GetEnumerator()) { if (enumerator.MoveNext()) { var previous =