public static SortOrder sortOrderFor(PartitionSpec spec) {
    if (spec.isUnpartitioned()) {
      return SortOrder.unsorted();
    }

    SortOrder.Builder builder = SortOrder.builderFor(spec.schema());
    SpecToOrderVisitor converter = new SpecToOrderVisitor(builder);
    PartitionSpecVisitor.visit(spec, converter);

    // columns used for bucketing are high cardinality; add one to the sort at the end
    String bucketColumn = converter.bucketColumn();
    if (bucketColumn != null) {
      builder.asc(bucketColumn);
    }

    return builder.build();
  }
Create a sort order that will group data for a partition spec.

<p>If the partition spec contains bucket columns, the sort order will also have a field to sort
by a column that is bucketed in the spec. The column is selected by the highest number of
buckets in the transform.

@param spec a partition spec
@return a sort order that will cluster data for the spec
iceberg/patched-iceberg-core/src/main/java/org/apache/iceberg/Partitioning.java