Example #1
0
        /// <summary>
        /// This is overridden to apply a sort based on the selected property descriptor and sort direction
        /// </summary>
        /// <param name="prop">The property descriptor to use for the sort values</param>
        /// <param name="direction">The direction of the sort</param>
        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            PropertyComparer <T> comparer = new PropertyComparer <T>(prop, direction);

            ((List <T>)base.Items).Sort(comparer);

            sortProperty  = prop;
            sortDirection = direction;
            isSorted      = true;

            this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }
Example #2
0
        //=====================================================================

        /// <summary>
        /// This compares the two items using the values obtained from the property descriptors and returns a
        /// value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare</param>
        /// <param name="y">The second object to compare</param>
        /// <returns>Returns less than zero when object X is less than object Y, zero if they are equal, greater
        /// than zero when object X is greater than object Y.</returns>
        public int Compare(T x, T y)
        {
            object value1, value2;
            int    result;

            // Get the value from the property in each object
            value1 = PropertyComparer <T> .GetPropertyValue(x, property.Name);

            value2 = PropertyComparer <T> .GetPropertyValue(y, property.Name);

            // Handle null values first
            if (value1 == null && value2 == null)
            {
                return(0);
            }

            if (value1 == null && value2 != null)
            {
                return((direction == ListSortDirection.Ascending) ?  -1 : 1);
            }

            if (value1 != null && value2 == null)
            {
                return((direction == ListSortDirection.Ascending) ? 1 : -1);
            }

            // Try comparing using IComparable.  If that won't work, see if they are equal.  If not, compare them
            // as strings.
            IComparable comp = value1 as IComparable;

            if (comp != null)
            {
                result = comp.CompareTo(value2);
            }
            else
            if (value1.Equals(value2))
            {
                result = 0;
            }
            else
            {
                result = String.Compare(value1.ToString(), value2.ToString(), StringComparison.Ordinal);
            }

            // Invert the result for descending order
            if (direction == ListSortDirection.Descending)
            {
                result *= -1;
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// This searches for the index of the item that has the specified property descriptor with the specified
        /// value.
        /// </summary>
        /// <param name="prop">The property descriptor used for the search</param>
        /// <param name="key">The value of the property to match</param>
        /// <returns>Returns the zero-based index of the item that matches the property descriptor and contains
        /// the specified value.</returns>
        protected override int FindCore(PropertyDescriptor prop, object key)
        {
            return(((List <T>)base.Items).FindIndex(0, base.Count, (x) =>
            {
                int result;

                object value = PropertyComparer <T> .GetPropertyValue(x, prop.Name);

                // Handle null values first
                if (key == null && value == null)
                {
                    return true;
                }

                if ((key == null && value != null) || (key != null && value == null))
                {
                    return false;
                }

                // Try comparing using IComparable.  If that won't work, see if they are equal.  If not,
                // compare them as strings.
                if (key is IComparable)
                {
                    result = ((IComparable)key).CompareTo(value);
                }
                else
                if (key.Equals(value))
                {
                    result = 0;
                }
                else
                {
                    result = String.Compare(key.ToString(), value.ToString(), StringComparison.Ordinal);
                }

                return (result == 0);
            }));
        }