System.Data.DataTable.Compare C# (CSharp) Method

Compare() private method

private Compare ( string s1, string s2 ) : int
s1 string
s2 string
return int
        internal int Compare(string s1, string s2) => Compare(s1, s2, null);

Same methods

DataTable::Compare ( string s1, string s2, CompareInfo comparer ) : int

Usage Example

Example #1
0
        internal long Compare(object vLeft, object vRight, Type type, int op)
        {
            //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "' , type " + type.ToString());
            long result       = 0;
            bool typeMismatch = false;

            try {
                if (type == typeof(UInt64))
                {
                    Decimal dec = Convert.ToDecimal(vLeft) - Convert.ToDecimal(vRight);
                    if (dec == 0)
                    {
                        result = 0;
                    }
                    else if (dec > 0)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                else if (type == typeof(char))
                {
                    result = Convert.ToInt32(vLeft) - Convert.ToInt32(Convert.ToChar(vRight));
                }
                else if (ExpressionNode.IsInteger(type))
                {
                    Int64 a = Convert.ToInt64(vLeft);
                    Int64 b = Convert.ToInt64(vRight);
                    checked { result = a - b; }
                }
                else if (type == typeof(Decimal))
                {
                    Decimal a = Convert.ToDecimal(vLeft);
                    Decimal b = Convert.ToDecimal(vRight);
                    result = Decimal.Compare(a, b);
                }
                else if (type == typeof(double))
                {
                    Double a = Convert.ToDouble(vLeft);
                    Double b = Convert.ToDouble(vRight);
                    double d;
                    checked { d = a - b; }
                    if (d == 0)
                    {
                        result = 0;
                    }
                    else if (d > 0)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                else if (type == typeof(Single))
                {
                    Single a = Convert.ToSingle(vLeft);
                    Single b = Convert.ToSingle(vRight);
                    Single d;
                    checked { d = a - b; }
                    if (d == 0)
                    {
                        result = 0;
                    }
                    else if (d > 0)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = -1;
                    }
                }
                else if (type == typeof(DateTime))
                {
                    //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "'");
                    result = DateTime.Compare(Convert.ToDateTime(vLeft), Convert.ToDateTime(vRight));
                    //Debug.WriteLine("result = " + result.ToString());
                }
                else if (type == typeof(string))
                {
                    //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "'");
                    result = table.Compare(Convert.ToString(vLeft), Convert.ToString(vRight), CompareOptions.None);
                    //Debug.WriteLine("result = " + result.ToString());
                }
                else if (type == typeof(Guid))
                {
                    //Debug.WriteLine("Compare '" + vLeft.ToString() + "' to '" + vRight.ToString() + "'");
                    result = ((Guid)vLeft).CompareTo((Guid)vRight);
                    //Debug.WriteLine("result = " + result.ToString());
                }
                else if (type == typeof(bool))
                {
                    if (op == Operators.EqualTo || op == Operators.NotEqual)
                    {
                        object bLeft  = DataExpression.ToBoolean(vLeft);
                        object bRight = DataExpression.ToBoolean(vRight);
                        result = Convert.ToInt32(bLeft) - Convert.ToInt32(bRight);
                    }
                    else
                    {
                        typeMismatch = true;
                    }
                }
                else
                {
                    typeMismatch = true;
                }
            }
            catch {
                SetTypeMismatchError(op, vLeft.GetType(), vRight.GetType());
            }

            if (typeMismatch)
            {
                SetTypeMismatchError(op, vLeft.GetType(), vRight.GetType());
            }

            return(result);
        }
DataTable