Go to the documentation of this file.00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #endregion
00020
00021 using System;
00022 using System.Collections.Generic;
00023 using System.Text;
00024
00025 namespace N2.Collections
00026 {
00033 public class ItemComparer<T> : IComparer<T> where T: ContentItem
00034 {
00035 #region Constructors
00036
00037 public ItemComparer()
00038 : this("SortOrder")
00039 {
00040 }
00041
00044 public ItemComparer(string sortExpression)
00045 {
00046 string[] pair = sortExpression.Split(' ');
00047 this.detailToCompare = pair[0];
00048 if (pair.Length > 1 && string.Compare(pair[1], "DESC", true) == 0)
00049 inverse = true;
00050 }
00051
00055 public ItemComparer(string detailToCompare, bool inverse)
00056 {
00057 this.detailToCompare = detailToCompare;
00058 this.inverse = inverse;
00059 }
00060 #endregion
00061
00062 #region Static Methods
00063
00064
00065
00066
00067
00068
00069 public static int Compare(T x, T y, string detailToCompare, bool inverse)
00070 {
00071 object ox = x[detailToCompare];
00072 object oy = y[detailToCompare];
00073 if (inverse)
00074 return System.Collections.Comparer.Default.Compare(oy, ox);
00075 else
00076 return System.Collections.Comparer.Default.Compare(ox, oy);
00077 }
00078 #endregion
00079
00080 #region IComparer & IComparer<T> Members
00081
00086 public int Compare(object x, object y)
00087 {
00088 if (x is T && y is T)
00089 return Compare((T)x, (T)y);
00090 return 0;
00091 }
00092
00097 public int Compare(T x, T y)
00098 {
00099 return Compare(x, y, DetailToCompare, Inverse);
00100 }
00101
00102 private string detailToCompare = "SortOrder";
00104 public string DetailToCompare
00105 {
00106 get { return detailToCompare; }
00107 set { detailToCompare = value; }
00108 }
00109
00110 bool inverse = false;
00112 public bool Inverse
00113 {
00114 get { return inverse; }
00115 set { inverse = value; }
00116 }
00117 #endregion
00118
00119 }
00120 }