public int CompareTo(XLRangeRow otherRow, IXLSortElements columnsToSort) { foreach (IXLSortElement e in columnsToSort) { var thisCell = (XLCell)Cell(e.ElementNumber); var otherCell = (XLCell)otherRow.Cell(e.ElementNumber); int comparison; bool thisCellIsBlank = thisCell.IsEmpty(); bool otherCellIsBlank = otherCell.IsEmpty(); if (e.IgnoreBlanks && (thisCellIsBlank || otherCellIsBlank)) { if (thisCellIsBlank && otherCellIsBlank) comparison = 0; else { if (thisCellIsBlank) comparison = e.SortOrder == XLSortOrder.Ascending ? 1 : -1; else comparison = e.SortOrder == XLSortOrder.Ascending ? -1 : 1; } } else { if (thisCell.DataType == otherCell.DataType) { if (thisCell.DataType == XLCellValues.Text) { comparison = e.MatchCase ? thisCell.InnerText.CompareTo(otherCell.InnerText) : String.Compare(thisCell.InnerText, otherCell.InnerText, true); } else if (thisCell.DataType == XLCellValues.TimeSpan) comparison = thisCell.GetTimeSpan().CompareTo(otherCell.GetTimeSpan()); else comparison = Double.Parse(thisCell.InnerText).CompareTo(Double.Parse(otherCell.InnerText)); } else if (e.MatchCase) comparison = String.Compare(thisCell.GetString(), otherCell.GetString(), true); else comparison = thisCell.GetString().CompareTo(otherCell.GetString()); } if (comparison != 0) return e.SortOrder == XLSortOrder.Ascending ? comparison : comparison * -1; } return 0; }
public int CompareTo(XLRangeRow otherRow, IXLSortElements columnsToSort) { foreach (IXLSortElement e in columnsToSort) { var thisCell = (XLCell)Cell(e.ElementNumber); var otherCell = (XLCell)otherRow.Cell(e.ElementNumber); int comparison; bool thisCellIsBlank = thisCell.IsEmpty(); bool otherCellIsBlank = otherCell.IsEmpty(); if (e.IgnoreBlanks && (thisCellIsBlank || otherCellIsBlank)) { if (thisCellIsBlank && otherCellIsBlank) { comparison = 0; } else { if (thisCellIsBlank) { comparison = e.SortOrder == XLSortOrder.Ascending ? 1 : -1; } else { comparison = e.SortOrder == XLSortOrder.Ascending ? -1 : 1; } } } else { if (thisCell.DataType == otherCell.DataType) { if (thisCell.DataType == XLDataType.Text) { comparison = e.MatchCase ? thisCell.InnerText.CompareTo(otherCell.InnerText) : String.Compare(thisCell.InnerText, otherCell.InnerText, true); } else if (thisCell.DataType == XLDataType.TimeSpan) { comparison = thisCell.GetTimeSpan().CompareTo(otherCell.GetTimeSpan()); } else { comparison = Double.Parse(thisCell.InnerText, XLHelper.NumberStyle, XLHelper.ParseCulture).CompareTo(Double.Parse(otherCell.InnerText, XLHelper.NumberStyle, XLHelper.ParseCulture)); } } else if (e.MatchCase) { comparison = String.Compare(thisCell.GetString(), otherCell.GetString(), true); } else { comparison = thisCell.GetString().CompareTo(otherCell.GetString()); } } if (comparison != 0) { return(e.SortOrder == XLSortOrder.Ascending ? comparison : comparison * -1); } } return(0); }
public int CompareTo(XLRangeRow otherRow, IXLSortElements columnsToSort) { foreach (IXLSortElement e in columnsToSort) { var thisCell = (XLCell)Cell(e.ElementNumber); var otherCell = (XLCell)otherRow.Cell(e.ElementNumber); int comparison; bool thisCellIsBlank = thisCell.IsEmpty(); bool otherCellIsBlank = otherCell.IsEmpty(); if (e.IgnoreBlanks && (thisCellIsBlank || otherCellIsBlank)) { if (thisCellIsBlank && otherCellIsBlank) { comparison = 0; } else { if (thisCellIsBlank) { comparison = e.SortOrder == XLSortOrder.Ascending ? 1 : -1; } else { comparison = e.SortOrder == XLSortOrder.Ascending ? -1 : 1; } } } else { if (thisCell.DataType == otherCell.DataType) { switch (thisCell.DataType) { case XLDataType.Text: comparison = e.MatchCase ? thisCell.InnerText.CompareTo(otherCell.InnerText) : String.Compare(thisCell.InnerText, otherCell.InnerText, true); break; case XLDataType.TimeSpan: comparison = thisCell.GetTimeSpan().CompareTo(otherCell.GetTimeSpan()); break; case XLDataType.DateTime: comparison = thisCell.GetDateTime().CompareTo(otherCell.GetDateTime()); break; case XLDataType.Number: comparison = thisCell.GetDouble().CompareTo(otherCell.GetDouble()); break; case XLDataType.Boolean: comparison = thisCell.GetBoolean().CompareTo(otherCell.GetBoolean()); break; default: throw new NotImplementedException(); } } else if (e.MatchCase) { comparison = String.Compare(thisCell.GetString(), otherCell.GetString(), true); } else { comparison = thisCell.GetString().CompareTo(otherCell.GetString()); } } if (comparison != 0) { return(e.SortOrder == XLSortOrder.Ascending ? comparison : -comparison); } } return(0); }