-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListViewItemComparer.cs
44 lines (41 loc) · 1.38 KB
/
ListViewItemComparer.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace D00B
{
class ListViewItemComparer : IComparer
{
private readonly int m_iColumn;
private readonly bool m_bAscending;
public ListViewItemComparer()
{
m_iColumn = 0;
m_bAscending = false;
}
public ListViewItemComparer(int iColumn, bool bAscending) : this()
{
m_iColumn = iColumn;
m_bAscending = bAscending;
}
public ListViewItemComparer(int iColumn)
{
m_iColumn = iColumn;
}
public int Compare(object lhs, object rhs)
{
string sLhs = ((ListViewItem)lhs).SubItems[m_iColumn].Text;
string sRhs = ((ListViewItem)rhs).SubItems[m_iColumn].Text;
bool bLhsNum = int.TryParse(sLhs, out int iLhs);
bool bRhsNum = int.TryParse(sRhs, out int iRhs);
bool bNumber = (bLhsNum && bRhsNum) ? true : false;
if (bNumber)
return m_bAscending ? (iLhs < iRhs ? -1 : (iLhs == iRhs ? 0 : 1)) : (iLhs < iRhs ? 1 : (iLhs == iRhs ? 0 : -1));
else
return m_bAscending ? string.Compare(sLhs, sRhs) : string.Compare(sRhs, sLhs); // Do we need insensitive?
}
}
}