code: protected PlanNode attachSorting( QueryContext context,
                                        PlanNode plan,
                                        List<? extends Ordering> orderings ) {
          if (orderings.isEmpty()) return plan;
          // remove all duplicates
          orderings = orderings.stream().distinct().collect(Collectors.toList());
          PlanNode sortNode = new PlanNode(Type.SORT);
  
          context.getHints().hasSort = true;
          sortNode.setProperty(Property.SORT_ORDER_BY, orderings);
          for (Ordering ordering : orderings) {
              sortNode.addSelectors(Visitors.getSelectorsReferencedBy(ordering));
          }
  
          sortNode.addLastChild(plan);
          return sortNode;
      }
docs: Attach SORT node at top of tree. The SORT may be pushed down to a source (or sources) if possible by the optimizer.
  
  @param context the context in which the query is being planned
  @param plan the existing plan
  @param orderings list of orderings from the query
  @return the updated plan, or the existing plan if there were no orderings; never null