icomparable

F# comparison vs C# IComparable

微笑、不失礼 提交于 2020-01-02 02:35:08
问题 My problem, in a nutshell, is this: What can I do about storing a tuple (or any type with a constraint of 'comparison') in a C# container that requires an IComparable ? This works: > let x (y : 'a when 'a : comparison) = y ;; val x : y:'a -> 'a when 'a : comparison > x (1,2) ;; val it : int * int = (1, 2) I would have thought this would work: > let x (y : IComparable<int>) = y ;; val x : y:IComparable<int> -> IComparable<int> > x (1,2) ;; x (1,2) ;; ---^^^ stdin(28,4): error FS0001: The type

Where is the inconsistency in this Icomparer that is causing a null reference?

为君一笑 提交于 2020-01-02 01:56:42
问题 I'm receiving a null object in my custom IComparer implementation despite no null entries in the collection it is being applied to. My understanding is this can be caused by inconsistencies in the IComparer implementation. I cannot spot where this could be happening in the following code. For reference the intent is that these are sorted by the 'correct' property first, then if they are the same, it sorts based on the 'tiebreakerDelta' property, which sorts closest to zero without going over.

What's the difference between IComparable & IEquatable interfaces?

痴心易碎 提交于 2019-12-28 08:02:10
问题 both the interfaces seem to compare objects for equality, so what's the major differences between them? 回答1: IEquatable tests whether two objects are equal. IComparable imposes a total ordering on the objects being compared. For example, IEquatable would tell you that 5 is not equal to 7. IComparable would tell you that 5 comes before 7. 回答2: IEquatable<T> for equality. IComparable<T> for ordering. 回答3: In addition to Greg D's answer: You might implement IComparable without implementing

How to create custom common number-text comparer in C# to sort numeric/string list?

时光总嘲笑我的痴心妄想 提交于 2019-12-25 08:59:35
问题 I have number-text list and I want to create common custom comparer logic to sort this list(s) using C#. E.g. var numericList = new List<string>{"100", "--", "-0.98", "N/A", "0.00", "-888"}; var stringList = new List<string> {"Smith", "--", "Peter", "", "Jim", "Ken", "NA"}; which contains some special characters like --, N/A,Space etc. And expected result after sort will be - For NemericList Ascending -> N/A, -- , -888 , -0.98 , 0.00 , 100 For StringList Ascending -> Jim, Ken, N/A, Peter,

Implementing custom comparison with CustomComparison and CustomEquality in F# tuple

扶醉桌前 提交于 2019-12-21 09:14:58
问题 I'm here to ask a specific topic - I really found few info about this on the web. I'm implementing a F# version of Minimax algorithm. The problem I'm having now is that I want to compare Leaf of my tree (data structure below). Searching the erros the VS gave to me I arrived to something like this: The tree type I used to have: type TreeOfPosition = | LeafP of Position | BranchP of Position * TreeOfPosition list and the temptative for implementing the IComparable type staticValue = int [

PowerShell IComparable with subclasses

…衆ロ難τιáo~ 提交于 2019-12-20 02:14:27
问题 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

How to Implement IComparable interface?

北慕城南 提交于 2019-12-17 03:04:43
问题 I am populating an array with instances of a class: BankAccount[] a; . . . a = new BankAccount[] { new BankAccount("George Smith", 500m), new BankAccount("Sid Zimmerman", 300m) }; Once I populate this array, I would like to sort it by balance amounts. In order to do that, I would like to be able to check whether each element is sortable using IComparable . I need to do this using interfaces. So far I have the following code: public interface IComparable { decimal CompareTo(BankAccount obj); }

Sort a group of Classes by default property

前提是你 提交于 2019-12-13 17:27:13
问题 TL;DR: Is there any way to pass a class collection/list to a library sorting algorithm, and get it to return a sorted list (preferably by a named/default class property)? I've recently been learning some Python, and was impressed by the Sorted() function, which can sort any iterable. For numbers this is straightforward, for classes however, it is possible to assign a comparison method like this. The method tells comparison operators how to compare 2 instances of the class. Amongst other

Embedded statement error [duplicate]

一曲冷凌霜 提交于 2019-12-13 10:14:57
问题 This question already has answers here : Variable declarations following if statements (4 answers) Closed 6 years ago . I have a simple custom list class and I am trying to implement IComparable to it, but it's not working to be honest. I tried MSDN and other blogs and still same. public class sortDateTime : IComparable { protected DateTime m_startDate, m_endDate; public DateTime startDate { get { return m_startDate; } set { m_startDate = startDate; } } public DateTime endDate { get { return

Implementing Icomparable on a Taxpayer class to sort based on taxOwed

北城以北 提交于 2019-12-13 09:23:34
问题 i am not being able to implemenet Icomparable CompareTo to compare Taxpayer objects based on tax owed ..can someone help me with the icomparable implementatioin of Taxpayer class?? I want to implement the icomparable like here-Interfaces is new topic to me..please help http://www.dotnetperls.com/icomparable using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Taxes { class Rates { // Create a class named rates that has the following data members: