コード例 #1
0
ファイル: UserResourceOrder.cs プロジェクト: mo5h/omeo
        public int CompareResources(IResource r1, IResource r2)
        {
            int propEquals           = 0;
            UserOrderSortSettings ss = _sortsettings;

            for (int i = 0; i < _sortsettings.SortProps.Length; i++)
            {
                // Compare against the user sort order (if that should be done before this step)
                if ((_hashUserOrder != null) &&
                    (ss != null) &&
                    (ss.ApplyUserOrderBefore == i) &&
                    ((propEquals = -_hashUserOrder.BucketComparer(r1.OriginalId, r2.OriginalId)) != 0))
                {
                    return(propEquals * (ss.UserOrderSortDirection ? 1 : -1));
                }

                // Compare the current pair of properties
                int propId = _sortsettings.SortProps[i];
                if (propId == ResourceProps.Type)
                {
                    propEquals = r1.Type.CompareTo(r2.Type);
                }
                else if (propId == ResourceProps.DisplayName)
                {
                    propEquals = String.Compare(r1.DisplayName, r2.DisplayName, true, CultureInfo.CurrentCulture);
                }
                else if (propId == ResourceProps.Id)
                {
                    propEquals = r1.Id - r2.Id;
                }
                else if (_propTypes[i] == PropDataType.Link)
                {
                    propEquals = r1.GetPropText(propId).CompareTo(r2.GetPropText(propId));
                }
                else
                {                 // Compare the property values, checking for the Null case also
                    IComparable cp;
                    object      prop1 = r1.GetProp(propId);
                    object      prop2 = r2.GetProp(propId);
                    if (prop1 == null)
                    {
                        propEquals = prop2 == null ? 0 : -1;
                    }
                    else if (prop2 == null)
                    {
                        propEquals = 1;
                    }
                    else if ((cp = prop1 as IComparable) != null)
                    {
                        propEquals = cp.CompareTo(prop2);
                    }
                }
                if (propEquals != 0)
                {
                    return(propEquals * (_sortsettings.SortDirections[i] ? 1 : -1));
                }
            }

            // If all the properties didn't rule out the sort order, check if the user order should be applied after all of them
            if ((_hashUserOrder != null) &&
                (ss != null) &&
                ((ss.ApplyUserOrderBefore < 0) || (ss.ApplyUserOrderBefore >= _sortsettings.SortProps.Length)) &&
                ((propEquals = -_hashUserOrder.BucketComparer(r1.OriginalId, r2.OriginalId)) != 0))
            {
                return(propEquals * (ss.UserOrderSortDirection ? 1 : -1));
            }

            return(0);
        }