private SelectResults buildSortedResult(CompiledSelect cs, int limit) throws QueryException {

    try {
      ExecutionContext localContext =
          new QueryExecutionContext(parameters, pr.getCache());


      List<Collection> allResults = new ArrayList<>();
      for (Collection<Collection> memberResults : resultsPerMember.values()) {
        for (Collection res : memberResults) {
          if (res != null) {
            allResults.add(res);
          }
        }
      }

      cumulativeResults = new NWayMergeResults(allResults, cs.isDistinct(), limit,
          cs.getOrderByAttrs(), localContext, cs.getElementTypeForOrderByQueries());
      return cumulativeResults;
    } catch (Exception ex) {
      throw new QueryException(
          "Unable to apply order-by on the partition region cumulative results.", ex);
    }

  }
Applies order-by on the results returned from PR nodes and puts the results in the cumulative
result set. The order-by is applied by running a generated query on the each result returned by
the remote nodes. Example generated query: SELECT DISTINCT * FROM $1 p ORDER BY p.ID Where
results are passed as bind parameter. This is added as quick turn-around, this is added based
on most commonly used queries, needs to be investigated further.
geode-core/src/main/java/org/apache/geode/internal/cache/PartitionedRegionQueryEvaluator.java