using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace BCG { /// /// Generic IComparer for IBlueRow. Accepts a list of column names to define the sort order. All included columns must be IComparable. /// public class BlueRowComparer : IComparer, IComparer { private IEnumerable myCompareColumns; private bool myIsAscending; public BlueRowComparer(bool ascending, params string[] columns) { this.myCompareColumns = columns; this.myIsAscending = ascending; } public BlueRowComparer(params string[] columns) : this(true, columns) { } public int Compare(IBlueRow x, IBlueRow y) { // Guard against nulls if (x == y) return 0; if (x == null) return -1; if (y == null) return 1; int result = 0; foreach (string compareColumn in this.myCompareColumns) { object left = x[compareColumn]; object right = y[compareColumn]; if (left == right) result = 0; else if (left == null || left == DBNull.Value) result = -1; else if (right == null || right == DBNull.Value) result = 1; else result = ((IComparable)left).CompareTo(right); if (result != 0) break; } return result * (this.myIsAscending ? 1 : -1); } #region IComparer Members public int Compare(object x, object y) { return this.Compare(x as IBlueRow, y as IBlueRow); } #endregion } }