-2

I have a class which overwrites the Compare method of IComarer class. Can you please explain me the code portion "return v1v2.CompareTo(v2v1)* -1;"? What * is representing here?Why we are subtracting the value by 1?

public class ValueComparator : System.Collections.IComparer
{
    public int Compare(Object lhs, object rhs)
    {
        string v1 = lhs.ToString();
        string v2 = rhs.ToString();
        string v1v2 = v1 + v2;
        string v2v1 = v2 + v1;
        return v1v2.CompareTo(v2v1)*  -1;

    }
}
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
Debanjan
  • 69
  • 1
  • 8
  • 3
    `*` is not substraction, it's multiplication. Not sure why it's doing that. It could have been written as `v2v1.CompareTo(v1v2)` instead, without the multiplication. – sstan Dec 11 '15 at 21:25
  • 4
    Its not a subtraction, its inverting the value. – Ron Beyer Dec 11 '15 at 21:25
  • Thanks Ron, Depending on the value of v1v2 and v2v1 v1v2.CompareTo(v2v1) may return >0, 0 OR <0. But after that I am not getting what the rest code portion is doing. The code is written to give the highest possible value of a given integer array int[] nums ={ 50, 5, 56 }; Array.Sort(nums, new ValueComparator()); – Debanjan Dec 11 '15 at 21:35

1 Answers1

3

It is essentially reversing the comparison. Since Compare returns -1 (less than), 0 (equal), or 1 (greater), by multiplying it by -1, it reverses the results -1 becomes 1, 0 remains 0, and 1 becomes -1.

As sstan mentioned, it probably should have written as v2v1.CompareTo(v1v2) instead.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • 2
    The bit about returning -1 or 1 is not entirely accurate. The [documentation](https://msdn.microsoft.com/en-us/library/system.icomparable.compareto%28v=vs.110%29.aspx) merely requires a negative or a positive value, not necessarily -1 or 1. – O. R. Mapper Dec 11 '15 at 21:32
  • 1
    Small qualm, it is not `-1`, `0`, `1` it is `< 0`, `0`, and `> 0` it is perfectly legal for the comparer to return `-275`, `0`, `600`. This looser restriction is beneficial because you can implement `int Compare(int a, int b) { return a - b; }` as simple subtractions instead of needing to involve boolean logic. – Scott Chamberlain Dec 11 '15 at 21:32
  • Importantly, this implementation is broken because `int.MinValue * -1` will still be `int.MinValue`. – Jon Skeet Dec 11 '15 at 21:35
  • 1
    @ScottChamberlain: "because you can implement `int Compare(int a, int b) { return a - b; }`" - well, you *should* not, unless you want to provoke overflows, but for smaller range types than `int`, that's a valid implementation for `Compare`. – O. R. Mapper Dec 11 '15 at 21:40
  • @O.R.Mapper true, I thought about that but did not write it down. Thanks for pointing it out. – Scott Chamberlain Dec 11 '15 at 21:41
  • Thanks everyone , I got my answer, This is basically to sort an arry such that is gives highest possible number "int[] nums ={ 50, 5, 56 }; Array.Sort(nums, new ValueComparator());" While comparing they are reversing the value... for example 505 is less that 550 so here 5 is bigger than 50 for this particular case!!! – Debanjan Dec 11 '15 at 21:48
  • Thanks for the clarifications. You are all of course correct, and should help anyone else who reads this in the future. – Robert McKee Dec 11 '15 at 22:03