1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Collections; |
6 |
using System.Windows.Forms; |
7 |
|
8 |
namespace RomCheater.Core |
9 |
{ |
10 |
/// <summary> |
11 |
/// This class is an implementation of the 'IComparer' interface. |
12 |
/// </summary> |
13 |
public class ListViewColumnSorter : IComparer |
14 |
{ |
15 |
/// <summary> |
16 |
/// Specifies the column to be sorted |
17 |
/// </summary> |
18 |
private int ColumnToSort; |
19 |
/// <summary> |
20 |
/// Specifies the order in which to sort (i.e. 'Ascending'). |
21 |
/// </summary> |
22 |
private SortOrder OrderOfSort; |
23 |
/// <summary> |
24 |
/// Case insensitive comparer object |
25 |
/// </summary> |
26 |
private CaseInsensitiveComparer ObjectCompare; |
27 |
|
28 |
/// <summary> |
29 |
/// Class constructor. Initializes various elements |
30 |
/// </summary> |
31 |
public ListViewColumnSorter() |
32 |
{ |
33 |
// Initialize the column to '0' |
34 |
ColumnToSort = 0; |
35 |
|
36 |
// Initialize the sort order to 'none' |
37 |
OrderOfSort = SortOrder.None; |
38 |
|
39 |
// Initialize the CaseInsensitiveComparer object |
40 |
ObjectCompare = new CaseInsensitiveComparer(); |
41 |
} |
42 |
|
43 |
/// <summary> |
44 |
/// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison. |
45 |
/// </summary> |
46 |
/// <param name="x">First object to be compared</param> |
47 |
/// <param name="y">Second object to be compared</param> |
48 |
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns> |
49 |
public int Compare(object x, object y) |
50 |
{ |
51 |
int compareResult; |
52 |
ListViewItem listviewX, listviewY; |
53 |
|
54 |
// Cast the objects to be compared to ListViewItem objects |
55 |
listviewX = (ListViewItem)x; |
56 |
listviewY = (ListViewItem)y; |
57 |
|
58 |
// Compare the two items |
59 |
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); |
60 |
|
61 |
// Calculate correct return value based on object comparison |
62 |
if (OrderOfSort == SortOrder.Ascending) |
63 |
{ |
64 |
// Ascending sort is selected, return normal result of compare operation |
65 |
return compareResult; |
66 |
} |
67 |
else if (OrderOfSort == SortOrder.Descending) |
68 |
{ |
69 |
// Descending sort is selected, return negative result of compare operation |
70 |
return (-compareResult); |
71 |
} |
72 |
else |
73 |
{ |
74 |
// Return '0' to indicate they are equal |
75 |
return 0; |
76 |
} |
77 |
} |
78 |
|
79 |
/// <summary> |
80 |
/// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0'). |
81 |
/// </summary> |
82 |
public int SortColumn |
83 |
{ |
84 |
set |
85 |
{ |
86 |
ColumnToSort = value; |
87 |
} |
88 |
get |
89 |
{ |
90 |
return ColumnToSort; |
91 |
} |
92 |
} |
93 |
|
94 |
/// <summary> |
95 |
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending'). |
96 |
/// </summary> |
97 |
public SortOrder Order |
98 |
{ |
99 |
set |
100 |
{ |
101 |
OrderOfSort = value; |
102 |
} |
103 |
get |
104 |
{ |
105 |
return OrderOfSort; |
106 |
} |
107 |
} |
108 |
|
109 |
} |
110 |
} |