Exemplo n.º 1
0
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other.
        /// </summary>
        /// <param name="x">First argument.</param>
        /// <param name="y">Second argument.</param>
        /// <returns>a value indicating whether one is less than, equal to or greater than the other.</returns>
        public int Compare(object obj0, object obj1)
        {
            IListItem item0 = (IListItem)obj0,
                      item1 = (IListItem)obj1;

            // If both objects are ThumbnailListItems - check for file/folder comparison.
            // Folders always should be first in sort order.
            ThumbnailListItem thumbnailListItem0 = obj0 as ThumbnailListItem,
                              thumbnailListItem1 = obj1 as ThumbnailListItem;

            if (thumbnailListItem0 != null && thumbnailListItem1 != null)
            {
                if (thumbnailListItem0.Pidl.Type == PidlType.File && thumbnailListItem1.Pidl.Type == PidlType.Folder)
                {
                    return(_sortAscending ? 1 : -1);
                }
                else if (thumbnailListItem0.Pidl.Type == PidlType.Folder && thumbnailListItem1.Pidl.Type == PidlType.File)
                {
                    return(_sortAscending ? -1 : 1);
                }
            }

            string str0 = item0.GetText(_textInfoId),
                   str1 = item1.GetText(_textInfoId);

            int result;

            if (_textInfoId == ThumbnailListItem.TextInfoIdFileSize)
            {
                try
                {
                    int int0 = StringToInt(str0);
                    int int1 = StringToInt(str1);

                    if (int0 > int1)
                    {
                        result = 1;
                    }
                    else if (int0 < int1)
                    {
                        result = -1;
                    }
                    else
                    {
                        result = 0;
                    }
                }
                catch
                {
                    result = str0.CompareTo(str1);
                }
            }
            else if (_textInfoId == ThumbnailListItem.TextInfoIdCreationDate)
            {
                try
                {
                    System.DateTime date0 = System.DateTime.Parse(str0, CultureInfo.CurrentCulture),
                                    date1 = System.DateTime.Parse(str1, CultureInfo.CurrentCulture);
                    result = date0.CompareTo(date1);
                }
                catch
                {
                    result = str0.CompareTo(str1);
                }
            }
            else
            {
                result = str0.CompareTo(str1);
            }

            if (_sortAscending)
            {
                return(result);
            }
            else
            {
                return(result * -1);
            }
        }