-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListViewItemComparer.cs
More file actions
102 lines (83 loc) · 2.57 KB
/
ListViewItemComparer.cs
File metadata and controls
102 lines (83 loc) · 2.57 KB
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
// ===========================================================================
// ©2013-2024 WebSupergoo. All rights reserved.
//
// This source code is for use exclusively with the ABCpdf product with
// which it is distributed, under the terms of the license for that
// product. Details can be found at
//
// http://www.websupergoo.com/
//
// This copyright notice must not be deleted and must be reproduced alongside
// any sections of code extracted from this module.
// ===========================================================================
using System;
using System.Collections;
using System.Windows.Forms;
using System.Globalization;
namespace WebSupergoo.PDFSurgeon
{
/// <summary>
/// Summary description for ListViewItemComparer.
/// </summary>
public class ListViewItemComparer: IComparer
{
private int _column;
private SortOrder _order;
private IComparer _stringComparer;
public ListViewItemComparer(){
_column = 0;
_order = SortOrder.None;
_stringComparer = new CaseInsensitiveComparer(
CultureInfo.InvariantCulture);
}
public int Column{
get{ return _column; }
set{ _column = value; }
}
public SortOrder Order{
get{ return _order; }
set{ _order = value; }
}
public int Compare(object x, object y){
if(_order==SortOrder.None)
return 0;
ListViewItem itemX = (ListViewItem)x;
ListViewItem itemY = (ListViewItem)y;
int v = 0;
if(_column==0 && TryCompareID(ref v, itemX, itemY))
return v;
return CompareStringColumn(itemX, itemY);
}
private bool TryCompareID(ref int outV, ListViewItem x, ListViewItem y){
if(_order==SortOrder.None){
outV = 0;
return true;
}
ObjectExtractor xExtractor = x.Tag as ObjectExtractor;
if(xExtractor==null)
return false;
ObjectExtractor yExtractor = y.Tag as ObjectExtractor;
if(yExtractor==null)
return false;
outV = Comparer.DefaultInvariant.Compare(
xExtractor.Object.ID, yExtractor.Object.ID);
if(_order==SortOrder.Descending)
outV = -outV;
return true;
}
private int CompareStringColumn(ListViewItem x, ListViewItem y){
if(_order==SortOrder.None)
return 0;
ListViewItem.ListViewSubItem sitemX = _column>=x.SubItems.Count?
null: x.SubItems[_column];
ListViewItem.ListViewSubItem sitemY = _column>=y.SubItems.Count?
null: y.SubItems[_column];
if(sitemX==sitemY)
return 0;
string xText = sitemX==null? "": sitemX.Text;
string yText = sitemY==null? "": sitemY.Text;
return xText=="" && yText==""? 0: xText==""? 1: yText==""? -1:
_stringComparer.Compare(sitemX.Text, sitemY.Text);
}
}
}