Ejemplo n.º 1
0
        public static int compareByValue(object o1, object o2)
        {
            if (o1 == o2)
            {
                return(0);
            }
            if (o1 == null)
            {
                return(-1);
            }
            if (o2 == null)
            {
                return(1);
            }

            IComparable c1 = (o1 as IComparable);

            if (c1 != null)
            {
                if (!(o2 is IComparable))
                {
                    return(-1);
                }

                if (o1 is Struct)
                {
                    if (!(o2 is Struct))
                    {
                        return(1);
                    }

                    return(c1.CompareTo((Struct)o2));
                }
                else if (o2 is Struct)
                {
                    return(-1);
                }

                if (o1.GetType() == o2.GetType())
                {
                    if (o1 is String)
                    {
                        return(String.Compare((string)o1, (string)o2, StringComparison.Ordinal));
                    }

                    return(c1.CompareTo(o2));
                }

                RuntimeType t1 = RuntimeTypeUtil.classify(o1);
                RuntimeType t2 = RuntimeTypeUtil.classify(o2);

                if (t1 != RuntimeType.UNKNOWN && t2 != RuntimeType.UNKNOWN)
                {
                    return(t1.CompareTo(t2));
                }

                return(((int)(o1.GetType().GetHashCode())).CompareTo(o2.GetType().GetHashCode()));
            }
            else if (o2 is IComparable)
            {
                return(1);
            }

            if (o1 is object[])
            {
                if (!(o2 is object[]))
                {
                    return(1);
                }

                object[] arr1 = (object[])o1;
                object[] arr2 = (object[])o2;
                int      l1   = arr1.Length;
                int      l2   = arr2.Length;
                int      l    = l1 < l2 ? l1 : l2;
                for (int i = 0; i < l; i++)
                {
                    int c = compareByValue(arr1[i], arr2[i]);
                    if (c != 0)
                    {
                        return(c);
                    }
                }

                return((l1 == l2) ? 0 : (l1 < l2 ? -1 : 1));
            }
            else if (o2 is object[])
            {
                return(-1);
            }

            if (o1 == o2)
            {
                return(0);
            }

            if (o1 is Delegate)
            {
                if (o1.Equals(o2))
                {
                    return(0);
                }
                else
                {
                    return(1);
                }
            }

            return(((int)o1.GetHashCode()).CompareTo(o2.GetHashCode()));
        }