forked from yavordimitrov/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericComparer.cs
More file actions
94 lines (84 loc) · 2.54 KB
/
Copy pathGenericComparer.cs
File metadata and controls
94 lines (84 loc) · 2.54 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
using System;
using System.Collections.Generic;
namespace SmartStore.Core
{
/// <summary>
/// This class is used to compare any
/// type(property) of a class for sorting.
/// This class automatically fetches the
/// type of the property and compares.
/// </summary>
public sealed partial class GenericComparer<T> : IComparer<T>
{
#region Enums
/// <summary>
/// The sort order direction for sorting the collection
/// </summary>
public enum SortOrder
{
/// <summary>
/// Ascending
/// </summary>
Ascending,
/// <summary>
/// Descending
/// </summary>
Descending
};
#endregion
#region Ctor
/// <summary>
/// Creates a new instance of the GenericComparer class
/// </summary>
/// <param name="sortColumn">The property on which the collection should be sorted</param>
/// <param name="sortingOrder">The direction of the sort</param>
public GenericComparer(string sortColumn, SortOrder sortingOrder)
{
this._sortColumn = sortColumn;
this._sortingOrder = sortingOrder;
}
#endregion
#region Fields
private readonly string _sortColumn;
private readonly SortOrder _sortingOrder;
#endregion
#region Properties
/// <summary>
/// Column Name(public property of the class) to be sorted.
/// </summary>
public string SortColumn
{
get { return _sortColumn; }
}
/// <summary>
/// Sorting order.
/// </summary>
public SortOrder SortingOrder
{
get { return _sortingOrder; }
}
#endregion
#region Methods
/// <summary>
/// Compare interface implementation
/// </summary>
/// <param name="x">custom Object</param>
/// <param name="y">custom Object</param>
/// <returns>int</returns>
public int Compare(T x, T y)
{
var propertyInfo = typeof(T).GetProperty(_sortColumn);
var obj1 = (IComparable)propertyInfo.GetValue(x, null);
var obj2 = (IComparable)propertyInfo.GetValue(y, null);
if (_sortingOrder == SortOrder.Ascending)
{
return (obj1.CompareTo(obj2));
}
else
{
return (obj2.CompareTo(obj1));
}
}
#endregion
}
}