-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLVSort.cpp
153 lines (138 loc) · 3.14 KB
/
LVSort.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "stdafx.h"
#include "LVSort.h"
//The CPP file will implement all the above:
/////////////////////////////////////////////////////////////////////////////
// CSortClass
CSortClass::CSortClass(CListCtrl * _pWnd, const int _iCol, const bool _bIsNumeric)
{
iCol = _iCol;
pWnd = _pWnd;
bIsNumeric = _bIsNumeric;
ASSERT(pWnd);
if (pWnd) {
int max = pWnd->GetItemCount();
DWORD dw;
CString txt;
if (bIsNumeric)
{
for (int t = 0; t < max; t++)
{
dw = pWnd->GetItemData(t);
txt = pWnd->GetItemText(t, iCol);
pWnd->SetItemData(t, (DWORD) new CSortItemInt(dw, txt));
}
}
else
{
for (int t = 0; t < max; t++)
{
dw = pWnd->GetItemData(t);
txt = pWnd->GetItemText(t, iCol);
pWnd->SetItemData(t, (DWORD) new CSortItem(dw, txt));
}
}
}
}
CSortClass::~CSortClass()
{
ASSERT(pWnd);
if (pWnd) {
int max = pWnd->GetItemCount();
if (bIsNumeric)
{
CSortItemInt * pItem;
for (int t = 0; t < max; t++)
{
pItem = (CSortItemInt *) pWnd->GetItemData(t);
ASSERT(pItem);
if (pItem) {
pWnd->SetItemData(t, pItem->dw);
delete pItem;
}
}
}
else
{
CSortItem * pItem;
for (int t = 0; t < max; t++)
{
pItem = (CSortItem *) pWnd->GetItemData(t);
ASSERT(pItem);
if (pItem) {
pWnd->SetItemData(t, pItem->dw);
delete pItem;
}
}
}
}
}
void
CSortClass::Sort(const bool bAsc)
{
if (bIsNumeric)
{
if (bAsc)
pWnd->SortItems(CompareAscI, 0L);
else
pWnd->SortItems(CompareDesI, 0L);
}
else
{
if (bAsc)
pWnd->SortItems(CompareAsc, 0L);
else
pWnd->SortItems(CompareDes, 0L);
}
}
int CALLBACK CSortClass::CompareAsc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CSortItem * i1 = (CSortItem *) lParam1;
CSortItem * i2 = (CSortItem *) lParam2;
ASSERT(i1 && i2);
if (i1 && i2)
return i1->txt.CompareNoCase(i2->txt);
else return NULL;
}
int CALLBACK CSortClass::CompareDes(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CSortItem * i1 = (CSortItem *) lParam1;
CSortItem * i2 = (CSortItem *) lParam2;
ASSERT(i1 && i2);
if (i1 && i2)
return i2->txt.CompareNoCase(i1->txt);
else return NULL;
}
int CALLBACK CSortClass::CompareAscI(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CSortItemInt * i1 = (CSortItemInt *) lParam1;
CSortItemInt * i2 = (CSortItemInt *) lParam2;
ASSERT(i1 && i2);
if (i1 && i2) {
if (i1->iInt == i2->iInt) return 0;
return i1->iInt > i2->iInt ? 1 : -1;
} else return NULL;
}
int CALLBACK CSortClass::CompareDesI(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CSortItemInt * i1 = (CSortItemInt *) lParam1;
CSortItemInt * i2 = (CSortItemInt *) lParam2;
ASSERT(i1 && i2);
if (i1 && i2) {
if (i1->iInt == i2->iInt) return 0;
return i1->iInt < i2->iInt ? 1 : -1;
}
return NULL;
}
CSortClass::CSortItem::CSortItem(const DWORD _dw, const CString & _txt)
{
dw = _dw;
txt = _txt;
}
CSortClass::CSortItem::~CSortItem()
{
}
CSortClass::CSortItemInt::CSortItemInt(const DWORD _dw, const CString & _txt)
{
iInt = atoi(_txt);
dw = _dw;
}