public override void VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, int index) { if (_groupingStatus == GroupingStatus.InGroupSubquery) { // Just ignore sorting before grouping takes place return; } if (_queryPartsAggregator.QueryType != N1QlQueryType.Array) { var orderByParts = orderByClause.Orderings.Select( ordering => string.Concat(GetN1QlExpression(ordering.Expression), " ", ordering.OrderingDirection.ToString().ToUpper())).ToList(); _queryPartsAggregator.AddOrderByPart(orderByParts); base.VisitOrderByClause(orderByClause, queryModel, index); } else { // This is an array subquery if (!VerifyArraySubqueryOrderByClause(orderByClause, queryModel, index)) { throw new NotSupportedException("N1Ql Array Subqueries Support One Ordering By The Array Elements Only"); } _queryPartsAggregator.AddWrappingFunction("ARRAY_SORT"); if (orderByClause.Orderings[0].OrderingDirection == OrderingDirection.Desc) { // There is no function to sort an array descending // so we just reverse the array after it's sorted ascending _queryPartsAggregator.AddWrappingFunction("ARRAY_REVERSE"); } } }