using System; using System.ComponentModel; using System.Collections.Generic; using System.Text; namespace bsmd.AISService.AIS { public class AIS_Target_Comparer : IComparer { private SortPropertyEnum sortProperty = SortPropertyEnum.NAME; private ListSortDirection sortDirection = ListSortDirection.Ascending; public enum SortPropertyEnum { NONE, MMSI, NAME, CALLSIGN, LASTUPDATE, STATION } #region Properties public ListSortDirection SortDirection { get { return this.sortDirection; } set { this.sortDirection = value; } } public SortPropertyEnum SortProperty { get { return this.sortProperty; } set { this.sortProperty = value; } } #endregion #region IComparer Members public int Compare(AIS_Target x, AIS_Target y) { switch (this.sortProperty) { case SortPropertyEnum.NONE: return 0; case SortPropertyEnum.NAME: { string xName = x.LastDBName; if (xName == null) xName = ""; string yName = y.LastDBName; if (yName == null) yName = ""; if (this.sortDirection == ListSortDirection.Ascending) return xName.CompareTo(yName); else return yName.CompareTo(xName); } case SortPropertyEnum.CALLSIGN: { string xCallsign = x.Callsign; if (xCallsign == null) xCallsign = ""; string yCallsign = y.Callsign; if (yCallsign == null) yCallsign = ""; if (this.sortDirection == ListSortDirection.Ascending) return xCallsign.CompareTo(yCallsign); else return yCallsign.CompareTo(xCallsign); } case SortPropertyEnum.LASTUPDATE: { DateTime xTime = x.LastUpdate ?? DateTime.MinValue; DateTime yTime = y.LastUpdate ?? DateTime.MinValue; if (this.sortDirection == ListSortDirection.Ascending) return xTime.CompareTo(yTime); else return yTime.CompareTo(xTime); } case SortPropertyEnum.MMSI: { if (this.sortDirection == ListSortDirection.Ascending) return x.MMSI.CompareTo(y.MMSI); else return y.MMSI.CompareTo(x.MMSI); } case SortPropertyEnum.STATION: { if (this.sortDirection == ListSortDirection.Ascending) return x.ReceivedFrom.CompareTo(y.ReceivedFrom); else return y.ReceivedFrom.CompareTo(x.ReceivedFrom); } default: return 0; } } #endregion } }