使用泛型排序,起到模板作用,提高了其复用性。
Code
public class IListSort<T>
{
private string _propertyName;
private bool _sortBy = true;
private IList<T> _list;
public IListSort(IList<T> list, string propertyName, bool sortBy)
{
_list = list;
_propertyName = propertyName;
_sortBy = sortBy;
}
public IListSort(IList<T> list, string propertyName)
{
_list = list;
_propertyName = propertyName;
_sortBy = true;
}
/**//// <summary>
/// IList
/// </summary>
public IList<T> List
{
get { return _list; }
set { _list = value; }
}
public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
public bool SortBy
{
get { return _sortBy; }
set { _sortBy = value; }
}
public IList<T> Sort()
{
if (_list.Count == 0) return _list;
for (int i = 1; i < _list.Count; i++)
{
T t = _list[i];
int j = i;
while ((j > 0) && Compare(_list[j - 1], t) < 0)
{
_list[j] = _list[j - 1];
--j;
}
_list[j] = t;
}
return _list;
}
private int Compare(T x, T y)
{
if (string.IsNullOrEmpty(_propertyName)) throw new ArgumentNullException("Non property's name!");
PropertyInfo property = typeof(T).GetProperty(_propertyName);
if (property == null) throw new ArgumentNullException("Unable of Property!");
switch (property.PropertyType.ToString())
{
case "System.Int32":
int int1 = 0;
int int2 = 0;
if (property.GetValue(x, null) != null)
{
int1 = Convert.ToInt32(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
int2 = Convert.ToInt32(property.GetValue(y, null));
}
if (_sortBy)
{
return int2.CompareTo(int1);
}
else
{
return int1.CompareTo(int2);
}
break;
case "System.Double":
double double1 = 0;
double double2 = 0;
if (property.GetValue(x, null) != null)
{
double1 = Convert.ToDouble(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
double2 = Convert.ToDouble(property.GetValue(y, null));
}
if (_sortBy)
{
return double2.CompareTo(double1);
}
else
{
return double1.CompareTo(double2);
}
break;
case "System.String":
string string1 = string.Empty;
string string2 = string.Empty;
if (property.GetValue(x, null) != null)
{
string1 = property.GetValue(x, null).ToString();
}
if (property.GetValue(y, null) != null)
{
string2 = property.GetValue(y, null).ToString();
}
if (_sortBy)
{
return string2.CompareTo(string1);
}
else
{
return string1.CompareTo(string2);
}
break;
case "System.DateTime":
DateTime DateTime1 = DateTime.Now;
DateTime DateTime2 = DateTime.Now;
if (property.GetValue(x, null) != null)
{
DateTime1 = Convert.ToDateTime(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
DateTime2 = Convert.ToDateTime(property.GetValue(y, null));
}
if (_sortBy)
{
return DateTime2.CompareTo(DateTime1);
}
else
{
return DateTime1.CompareTo(DateTime2);
}
break;
case "System.Decimal":
decimal decimal1 = decimal.Zero;
decimal decimal2 = decimal.Zero;
if (property.GetValue(x, null) != null)
{
decimal1 = Convert.ToDecimal(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
decimal2 = Convert.ToDecimal(property.GetValue(y, null));
}
if (_sortBy)
{
return decimal2.CompareTo(decimal1);
}
else
{
return decimal1.CompareTo(decimal2);
}
break;
}
return 0;
}
}
public class IListSort<T>
{
private string _propertyName;
private bool _sortBy = true;
private IList<T> _list;
public IListSort(IList<T> list, string propertyName, bool sortBy)
{
_list = list;
_propertyName = propertyName;
_sortBy = sortBy;
}
public IListSort(IList<T> list, string propertyName)
{
_list = list;
_propertyName = propertyName;
_sortBy = true;
}
/**//// <summary>
/// IList
/// </summary>
public IList<T> List
{
get { return _list; }
set { _list = value; }
}
public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
public bool SortBy
{
get { return _sortBy; }
set { _sortBy = value; }
}
public IList<T> Sort()
{
if (_list.Count == 0) return _list;
for (int i = 1; i < _list.Count; i++)
{
T t = _list[i];
int j = i;
while ((j > 0) && Compare(_list[j - 1], t) < 0)
{
_list[j] = _list[j - 1];
--j;
}
_list[j] = t;
}
return _list;
}
private int Compare(T x, T y)
{
if (string.IsNullOrEmpty(_propertyName)) throw new ArgumentNullException("Non property's name!");
PropertyInfo property = typeof(T).GetProperty(_propertyName);
if (property == null) throw new ArgumentNullException("Unable of Property!");
switch (property.PropertyType.ToString())
{
case "System.Int32":
int int1 = 0;
int int2 = 0;
if (property.GetValue(x, null) != null)
{
int1 = Convert.ToInt32(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
int2 = Convert.ToInt32(property.GetValue(y, null));
}
if (_sortBy)
{
return int2.CompareTo(int1);
}
else
{
return int1.CompareTo(int2);
}
break;
case "System.Double":
double double1 = 0;
double double2 = 0;
if (property.GetValue(x, null) != null)
{
double1 = Convert.ToDouble(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
double2 = Convert.ToDouble(property.GetValue(y, null));
}
if (_sortBy)
{
return double2.CompareTo(double1);
}
else
{
return double1.CompareTo(double2);
}
break;
case "System.String":
string string1 = string.Empty;
string string2 = string.Empty;
if (property.GetValue(x, null) != null)
{
string1 = property.GetValue(x, null).ToString();
}
if (property.GetValue(y, null) != null)
{
string2 = property.GetValue(y, null).ToString();
}
if (_sortBy)
{
return string2.CompareTo(string1);
}
else
{
return string1.CompareTo(string2);
}
break;
case "System.DateTime":
DateTime DateTime1 = DateTime.Now;
DateTime DateTime2 = DateTime.Now;
if (property.GetValue(x, null) != null)
{
DateTime1 = Convert.ToDateTime(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
DateTime2 = Convert.ToDateTime(property.GetValue(y, null));
}
if (_sortBy)
{
return DateTime2.CompareTo(DateTime1);
}
else
{
return DateTime1.CompareTo(DateTime2);
}
break;
case "System.Decimal":
decimal decimal1 = decimal.Zero;
decimal decimal2 = decimal.Zero;
if (property.GetValue(x, null) != null)
{
decimal1 = Convert.ToDecimal(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
decimal2 = Convert.ToDecimal(property.GetValue(y, null));
}
if (_sortBy)
{
return decimal2.CompareTo(decimal1);
}
else
{
return decimal1.CompareTo(decimal2);
}
break;
}
return 0;
}
}