public int Compare(int x, int y) { int result = 0; double double1, double2; DateTime dateTime1, dateTime2; TimeSpan timeSpan1, timeSpan2; string string1, string2; if (matrix == null) { throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null."); } if (sortedIndices == null) { return(0); } foreach (KeyValuePair <int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) { string1 = matrix.GetValue(x, pair.Key); string2 = matrix.GetValue(y, pair.Key); if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2)) { result = double1.CompareTo(double2); } else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2)) { result = dateTime1.CompareTo(dateTime2); } else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2)) { result = timeSpan1.CompareTo(timeSpan2); } else { if (string1 != null) { result = string1.CompareTo(string2); } else if (string2 != null) { result = string2.CompareTo(string1) * -1; } } if (pair.Value == SortOrder.Descending) { result *= -1; } if (result != 0) { return(result); } } return(result); }
public void AddItemToBuilder(IItem item, string name, SolutionMessage.Builder builder) { IStringConvertibleValue value = (item as IStringConvertibleValue); if (value != null) { SolutionMessage.Types.StringVariable.Builder var = SolutionMessage.Types.StringVariable.CreateBuilder(); var.SetName(name).SetData(value.GetValue()); builder.AddStringVars(var.Build()); } else { IStringConvertibleArray array = (item as IStringConvertibleArray); if (array != null) { SolutionMessage.Types.StringArrayVariable.Builder var = SolutionMessage.Types.StringArrayVariable.CreateBuilder(); var.SetName(name).SetLength(array.Length); for (int i = 0; i < array.Length; i++) { var.AddData(array.GetValue(i)); } builder.AddStringArrayVars(var.Build()); } else { IStringConvertibleMatrix matrix = (item as IStringConvertibleMatrix); if (matrix != null) { SolutionMessage.Types.StringArrayVariable.Builder var = SolutionMessage.Types.StringArrayVariable.CreateBuilder(); var.SetName(name).SetLength(matrix.Columns); for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { var.AddData(matrix.GetValue(i, j)); } } builder.AddStringArrayVars(var.Build()); } else { throw new ArgumentException(ItemName + ": Item is not of a supported type.", "item"); } } } }