/// <summary> /// Verify that the results are ordered accoring to the specified ordering. /// </summary> /// <param name="ordering">The ordering to verify by.</param> /// <param name="elements">The elements on which to verify the ordering.</param> /// <returns>A value indicating whether the collection is properly sorted.</returns> public static bool IsOrdered(QueryOrdering ordering, IEnumerable <object> elements) { ExceptionUtilities.CheckArgumentNotNull(ordering, "ordering"); QueryScalarValue[][] sortedSelectorsTable = BuildSortedSelectorsTable(ordering, elements); int totalRows = sortedSelectorsTable.GetLength(0); for (int row = 0; row < totalRows - 1; row++) { if (!RowsAreOrdered(sortedSelectorsTable[row], sortedSelectorsTable[row + 1], ordering)) { return(false); } } return(true); }
/// <summary> /// Builds a table of sorted orderby primitive values, where the rows by the size of result set, the columns by the number of orderby key selectors. /// </summary> /// <param name="ordering">query ordering of order by key selectors</param> /// <param name="elements">Elements used to get the table values.</param> /// <returns>the table of sorted orderby/thenby scalar values. </returns> private static QueryScalarValue[][] BuildSortedSelectorsTable(QueryOrdering ordering, IEnumerable <object> elements) { int orderingSelectorsCount = ordering.Selectors.Count(); int selectorRowCount = elements.Count(); var orderingSelectorsTable = new QueryScalarValue[selectorRowCount][]; for (int i = 0; i < selectorRowCount; i++) { orderingSelectorsTable[i] = new QueryScalarValue[orderingSelectorsCount]; } for (int row = 0; row < selectorRowCount; row++) { var currentRow = elements.ElementAt(row); for (int col = 0; col < orderingSelectorsCount; col++) { var selector = ordering.Selectors.ElementAt(col); orderingSelectorsTable[row][col] = selector(currentRow); } } return(orderingSelectorsTable); }
private static bool RowsAreOrdered(QueryScalarValue[] selectorsRow1, QueryScalarValue[] selectorsRow2, QueryOrdering ordering) { var verifyNextColumn = true; var col = 0; while (verifyNextColumn && col < selectorsRow1.Length) { verifyNextColumn = false; var isDecending = ordering.AreDescending.ElementAt(col); var compareResult = QueryScalarValue.Comparer.Compare(selectorsRow1[col], selectorsRow2[col]); if (compareResult == 0) { verifyNextColumn = true; } if (isDecending) { if (compareResult < 0) { return(false); } } else { if (compareResult > 0) { return(false); } } col++; } return(true); }