icomparable

How do I use the IComparable interface?

我的未来我决定 提交于 2021-02-05 20:23:22
问题 I need a basic example of how to use the IComparable interface so that I can sort in ascending or descending order and by different fields of the object type I'm sorting. 回答1: Well, since you are using List<T> it would be a lot simpler to just use a Comparison<T> , for example: List<Foo> data = ... // sort by name descending data.Sort((x,y) => -x.Name.CompareTo(y.Name)); Of course, with LINQ you could just use: var ordered = data.OrderByDescending(x=>x.Name); But you can re-introduce this in

How do I use the IComparable interface?

╄→гoц情女王★ 提交于 2021-02-05 20:22:44
问题 I need a basic example of how to use the IComparable interface so that I can sort in ascending or descending order and by different fields of the object type I'm sorting. 回答1: Well, since you are using List<T> it would be a lot simpler to just use a Comparison<T> , for example: List<Foo> data = ... // sort by name descending data.Sort((x,y) => -x.Name.CompareTo(y.Name)); Of course, with LINQ you could just use: var ordered = data.OrderByDescending(x=>x.Name); But you can re-introduce this in

Sort ArrayList with two dimensional objects

落花浮王杯 提交于 2021-01-28 21:07:10
问题 I have an ArrayList where I store 10 objects with two random generated numbers each and I want to sort the list by the Y values of the objects. Random rndm = new Random(); ArrayList CustomList = new ArrayList(); for (int i = 0; i < 10; i++) { Point LP = new Point(rndm.Next(50), rndm.Next(50)); CustomList.Add(LP); } PrintList(CustomList); CustomList.Sort(); PrintList(CustomList); The Sort method throws the following exception: System.InvalidOperationException: The comparer threw an exception.

Comparing two objects with default IComparable implementation with F# Reflection

橙三吉。 提交于 2020-04-16 05:47:22
问题 Given two objects in F#, is there a way to use their IComparable method to compare them, assuming they are both of the same sub-type and that IComparable is implemented for their common sub-type. What I am trying to achieve in pseudo-code : let tycompare (o1 : obj) (o2 : obj) : int option = let (ty1, ty2) = (o1.GetType(), o2.GetType()) if ty1 <> ty2 then None else if IComparable is implemented on ty1 then o1.CompareTo(o2) |> Some else None I am aware of this post but I do not think it helps

System.Version doesn't implement System.IComparable in F#

醉酒当歌 提交于 2020-01-15 08:29:10
问题 I want to sort a sequence of Version objects in F#: let maxVersion = versions |> Seq.max (fun version -> version) The compiler produces the following error message: The type '(seq -> 'a)' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface When I hit F12 in Visual studio to take a look at the metadata of Version it says that Version only implements ICloneable , but not IComparable . But when I go to sourceof.net it says it

Sort a list of interface objects

走远了吗. 提交于 2020-01-14 19:05:29
问题 I have a couple of classes that implement an interface, IFoo. I need to display a list of objects of these classes in a table, which I'd like to be able to sort by any arbitrary column in the table. So, the data source for the table is a List<IFoo> . The problem I've run into is that the standard way of implementing IComparable or IComparer for objects that are used in a list requires a static method, but static methods aren't allowed in interfaces. So, the question boils down to this: how

Sorting IComparable objects some of which are null

寵の児 提交于 2020-01-14 08:56:09
问题 Most people, when writing a refence type (class) which implements IComparable<T>, use the convention that null is LESS than any actual object. But if you try to use the opposite convention, something interesting happens: using System; using System.Collections.Generic; namespace SortingNulls { internal class Child : IComparable<Child> { public int Age; public string Name; public int CompareTo(Child other) { if (other == null) return -1; // what's your problem? return this.Age.CompareTo(other

C# - How to implement multiple comparers for an IComparable<T> class?

一笑奈何 提交于 2020-01-10 10:14:29
问题 I have a class that implements IComparable. public class MyClass : IComparable<MyClass> { public int CompareTo(MyClass c) { return this.whatever.CompareTo(c.whatever); } etc.. } I then can call the sort method of a generic list of my class List<MyClass> c = new List<MyClass>(); //Add stuff, etc. c.Sort(); and have the list sorted according to my comparer. How do i specify further comparers to sort my collection different ways according to the other properties of MyClass in order to let users

Sortedset not using custom equals

流过昼夜 提交于 2020-01-07 07:24:13
问题 My class implements IEquatable and IComparable. It then gets added to a sortedset. The goal is to have it be sorted by its "Date" property and be equal if both "ID1" and "ID2" are the same. The implemented methods: public int CompareTo(MyClass other) { return other.Date.CompareTo(Date); } public override int GetHashCode() { unchecked { var hashCode = ID1; hashCode = (hashCode * 397) ^ ID2; return hashCode; } } public bool Equals(MyClass other) { if (ReferenceEquals(null, other)) return false;

CompareTo method not working, it won't get AlbumName from the Album class- IComparable issue I think

馋奶兔 提交于 2020-01-05 03:47:25
问题 I'm trying to get my CompareTo method to from Artist class to work, however it won't get AlbumName from the Album class. I'm pretty sure it's an issue with IComparable on the Album class. Please help with code specific answers, thankyou. Error 1 'Assignment.Album' does not implement interface member 'System.IComparable.CompareTo(object)' Album Class: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment { class Album : IComparable { public