This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListViewSorter.cs
55 lines (45 loc) · 1.52 KB
/
ListViewSorter.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
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace templates
{
public class ListViewSorter : IComparer
{
public int ByColumn { get; set; }
public int LastSort { get; set; }
#region IComparer Members
public int Compare(object o1, object o2)
{
if (!(o1 is ListViewItem))
return (0);
if (!(o2 is ListViewItem))
return (0);
var lvi1 = (ListViewItem) o2;
string str1 = lvi1.SubItems[ByColumn].Text;
var lvi2 = (ListViewItem) o1;
string str2 = lvi2.SubItems[ByColumn].Text;
int result = 0;
//test to see if the string is a number - perform an int compare instead of a string compare
if (Regex.IsMatch(str1, "^[0-9]+$") && Regex.IsMatch(str2, "^[0-9]+$"))
{
int s1 = int.Parse(str1);
int s2 = int.Parse(str2);
if (lvi1.ListView.Sorting == SortOrder.Ascending)
result = s1.CompareTo(s2);
else
result = s2.CompareTo(s1);
}
else
{
if (lvi1.ListView.Sorting == SortOrder.Ascending)
result = String.Compare(str1, str2);
else
result = String.Compare(str2, str1);
}
LastSort = ByColumn;
return (result);
}
#endregion
}
}