@Override
  public void toData(DataOutput out,
      SerializationContext context) throws IOException {
    boolean isStruct = collectionType.getElementType().isStructType();
    context.getSerializer().writeObject(collectionType.getElementType(), out);
    DataSerializer.writePrimitiveBoolean(isDistinct, out);
    HeapDataOutputStream hdos = new HeapDataOutputStream(1024, null);
    LongUpdater lu = hdos.reserveLong();
    Iterator<E> iter = iterator();
    int numElements = 0;
    while (iter.hasNext()) {
      E data = iter.next();
      if (isStruct) {
        Object[] fields = ((Struct) data).getFieldValues();
        DataSerializer.writeObjectArray(fields, hdos);
      } else {
        context.getSerializer().writeObject(data, hdos);
      }
      ++numElements;
    }
    lu.update(numElements);
    hdos.sendTo(out);

  }
The n - way merge results returns a sorted results on the cumulative sorted results for
partitioned region based query
geode-core/src/main/java/org/apache/geode/cache/query/internal/NWayMergeResults.java