- using System;
- using System.Data;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Collections.Generic;
- using System.Reflection;
-
- namespace Tests
- {
- /// <sumary>
- /// Type permettant de spécifier le sens du tri
- /// <sumary>
- public enum OrderType : int { Asc = 1, Desc = -1 }
-
- /// <sumary>
- /// Interface à implémenter pour la gestion de collections triables
- /// <sumary>
- public interface IOrderableCollection
- {
- /// <sumary>
- /// Obtient ou définit la propriété sur laquelle la collection est triée
- /// <sumary>
- PropertyInfo PropertyOrder { get; set; }
- /// <sumary>
- /// Obtient ou définit le sens du tri
- /// <sumary>
- OrderType OrderType { get; set; }
- /// <sumary>
- /// Tri la liste en fonction du nom du champ et du sens de tri
- /// <sumary>
- void Sort(string fieldName, OrderType orderType);
- }
-
- /// <sumary>
- /// Exemple d'implémentation sur une collection generics
- /// <sumary>
- public class OrderableList<T> : List<T>, IOrderableCollection
- {
- public PropertyInfo PropertyOrder { get; set; }
- public OrderType OrderType { get; set; }
-
- public void Sort(string fieldName, OrderType orderType)
- {
- this.OrderType = orderType;
- this.PropertyOrder = typeof(T).GetProperty(fieldName);
- if (this.PropertyOrder != null) this.Sort(Compare);
- }
-
- private int Compare(T t1, T t2)
- {
- object obj1 = this.PropertyOrder.GetValue(t1, null);
- object obj2 = this.PropertyOrder.GetValue(t2, null);
-
- IComparable obj1Comparable = obj1 as IComparable;
- if (obj1Comparable != null)
- return obj1Comparable.CompareTo(obj2) * (int)this.OrderType;
- return 0;
- }
- }
- }
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Reflection;
namespace Tests
{
/// <sumary>
/// Type permettant de spécifier le sens du tri
/// <sumary>
public enum OrderType : int { Asc = 1, Desc = -1 }
/// <sumary>
/// Interface à implémenter pour la gestion de collections triables
/// <sumary>
public interface IOrderableCollection
{
/// <sumary>
/// Obtient ou définit la propriété sur laquelle la collection est triée
/// <sumary>
PropertyInfo PropertyOrder { get; set; }
/// <sumary>
/// Obtient ou définit le sens du tri
/// <sumary>
OrderType OrderType { get; set; }
/// <sumary>
/// Tri la liste en fonction du nom du champ et du sens de tri
/// <sumary>
void Sort(string fieldName, OrderType orderType);
}
/// <sumary>
/// Exemple d'implémentation sur une collection generics
/// <sumary>
public class OrderableList<T> : List<T>, IOrderableCollection
{
public PropertyInfo PropertyOrder { get; set; }
public OrderType OrderType { get; set; }
public void Sort(string fieldName, OrderType orderType)
{
this.OrderType = orderType;
this.PropertyOrder = typeof(T).GetProperty(fieldName);
if (this.PropertyOrder != null) this.Sort(Compare);
}
private int Compare(T t1, T t2)
{
object obj1 = this.PropertyOrder.GetValue(t1, null);
object obj2 = this.PropertyOrder.GetValue(t2, null);
IComparable obj1Comparable = obj1 as IComparable;
if (obj1Comparable != null)
return obj1Comparable.CompareTo(obj2) * (int)this.OrderType;
return 0;
}
}
}