Advertisement
fortsoft

ListViewSorter

Mar 23rd, 2021 (edited)
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | Source Code | 0 0
  1. /**
  2.  * This is open-source software licensed under the terms of the MIT License.
  3.  *
  4.  * Copyright (c) 2012-2023 Petr Červinka - FortSoft <[email protected]>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in all
  14.  * copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  **
  24.  * Version 1.1.1.0
  25.  */
  26.  
  27. using System;
  28. using System.Collections;
  29. using System.Windows.Forms;
  30.  
  31. namespace FortSoft.Tools {
  32.  
  33.     /// <summary>
  34.     /// Implements a custom ListView sorter sorting items by the Tag property.
  35.     /// </summary>
  36.     public sealed class ListViewSorter : IComparer {
  37.  
  38.         /// <summary>
  39.         /// Initializes a new instance of the <see cref="ListViewSorter"/> class.
  40.         /// </summary>
  41.         public ListViewSorter() {
  42.             SortOrder = SortOrder.None;
  43.         }
  44.  
  45.         /// <summary>
  46.         /// Sort column.
  47.         /// </summary>
  48.         public int SortColumn { get; set; }
  49.  
  50.         /// <summary>
  51.         /// Sort order.
  52.         /// </summary>
  53.         public SortOrder SortOrder { get; set; }
  54.  
  55.         /// <summary>
  56.         /// The method performing the comparison.
  57.         /// </summary>
  58.         public int Compare(object a, object b) {
  59.             ListViewItem listViewItemA = (ListViewItem)a;
  60.             ListViewItem listViewItemB = (ListViewItem)b;
  61.             int result = 0;
  62.             if (listViewItemA.SubItems.Count > SortColumn && listViewItemB.SubItems.Count > SortColumn) {
  63.                 object objectA = listViewItemA.SubItems[SortColumn].Tag;
  64.                 object objectB = listViewItemB.SubItems[SortColumn].Tag;
  65.                 if (objectA != null && objectB != null) {
  66.                     Type type = objectA.GetType();
  67.                     if (type.Equals(objectB.GetType())) {
  68.                         if (type == typeof(string)) {
  69.                             result = ((string)objectA).CompareTo((string)objectB);
  70.                         } else if (type == typeof(int)) {
  71.                             result = (int)objectA > (int)objectB ? 1 : (int)objectA < (int)objectB ? -1 : 0;
  72.                         } else if (type == typeof(uint)) {
  73.                             result = (uint)objectA > (uint)objectB ? 1 : (uint)objectA < (uint)objectB ? -1 : 0;
  74.                         } else if (type == typeof(short)) {
  75.                             result = (short)objectA > (short)objectB ? 1 : (short)objectA < (short)objectB ? -1 : 0;
  76.                         } else if (type == typeof(ushort)) {
  77.                             result = (ushort)objectA > (ushort)objectB ? 1 : (ushort)objectA < (ushort)objectB ? -1 : 0;
  78.                         } else if (type == typeof(long)) {
  79.                             result = (long)objectA > (long)objectB ? 1 : (long)objectA < (long)objectB ? -1 : 0;
  80.                         } else if (type == typeof(ulong)) {
  81.                             result = (ulong)objectA > (ulong)objectB ? 1 : (ulong)objectA < (ulong)objectB ? -1 : 0;
  82.                         } else if (type == typeof(float)) {
  83.                             result = (float)objectA > (float)objectB ? 1 : (float)objectA < (float)objectB ? -1 : 0;
  84.                         } else if (type == typeof(decimal)) {
  85.                             result = (decimal)objectA > (decimal)objectB ? 1 : (decimal)objectA < (decimal)objectB ? -1 : 0;
  86.                         } else if (type == typeof(DateTime)) {
  87.                             result = (DateTime)objectA > (DateTime)objectB ? 1 : (DateTime)objectA < (DateTime)objectB ? -1 : 0;
  88.                         } else if (type == typeof(TimeSpan)) {
  89.                             result = (TimeSpan)objectA > (TimeSpan)objectB ? 1 : (TimeSpan)objectA < (TimeSpan)objectB ? -1 : 0;
  90.                         }
  91.                     }
  92.                 } else {
  93.                     result = (listViewItemA.SubItems[SortColumn].Text).CompareTo(listViewItemB.SubItems[SortColumn].Text);
  94.                 }
  95.             }
  96.             switch (SortOrder) {
  97.                 case SortOrder.Ascending:
  98.                     return result;
  99.                 case SortOrder.Descending:
  100.                     return -result;
  101.                 default:
  102.                     return 0;
  103.             }
  104.         }
  105.     }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement