-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathColumnSort.cs
103 lines (92 loc) · 3.63 KB
/
ColumnSort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Windows.Forms;
/// <summary>
/// A custom comparer for sorting ListView columns, implementing the 'IComparer' interface.
/// </summary>
public class ListViewColumnSorter : IComparer
{
/// <summary>
/// Case-insensitive comparer object used for comparing strings.
/// </summary>
private readonly CaseInsensitiveComparer ObjectCompare;
/// <summary>
/// Initializes a new instance of the ListViewColumnSorter class and sets default sorting parameters.
/// </summary>
public ListViewColumnSorter()
{
// Default column index for sorting
SortColumn = 0;
// Default sorting order
Order = SortOrder.Ascending;
// Initialize the case-insensitive comparer
ObjectCompare = new CaseInsensitiveComparer();
}
/// <summary>
/// Compares two ListViewItem objects based on the specified column index and sorting order.
/// </summary>
/// <param name="x">First ListViewItem object to compare.</param>
/// <param name="y">Second ListViewItem object to compare.</param>
/// <returns>
/// A signed integer indicating the relative values of x and y:
/// "0" if equal, a negative number if x is less than y, and a positive number if x is greater than y.
/// </returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX = (ListViewItem)x;
ListViewItem listviewY = (ListViewItem)y;
// Determine if the column requires numeric comparison
if (SortColumn == 3 || SortColumn == 5) // Numeric columns: VersionCodeIndex, VersionNameIndex
{
try
{
// Parse and compare numeric values directly
int xNum = ParseNumber(listviewX.SubItems[SortColumn].Text);
int yNum = ParseNumber(listviewY.SubItems[SortColumn].Text);
// Compare numerically
compareResult = xNum.CompareTo(yNum);
}
catch
{
// Fallback to string comparison if parsing fails
compareResult = ObjectCompare.Compare(listviewX.SubItems[SortColumn].Text, listviewY.SubItems[SortColumn].Text);
}
}
else
{
// Default to string comparison for non-numeric columns
compareResult = ObjectCompare.Compare(listviewX.SubItems[SortColumn].Text, listviewY.SubItems[SortColumn].Text);
}
// Determine the return value based on the specified sort order
if (Order == SortOrder.Ascending)
{
return compareResult;
}
else if (Order == SortOrder.Descending)
{
return -compareResult;
}
else
{
return 0; // Indicate equality
}
}
/// <summary>
/// Parses a numeric value from a string for accurate numeric comparison.
/// </summary>
/// <param name="text">The string representation of the number.</param>
/// <returns>The parsed integer value; returns 0 if parsing fails.</returns>
private int ParseNumber(string text)
{
// Directly attempt to parse the string as an integer
return int.TryParse(text, out int result) ? result : 0;
}
/// <summary>
/// Gets or sets the index of the column to be sorted (default is '0').
/// </summary>
public int SortColumn { get; set; }
/// <summary>
/// Gets or sets the order of sorting (Ascending or Descending).
/// </summary>
public SortOrder Order { get; set; }
}