public static List<String> orderFields(List<String> fields, List<SortSpec> sorts) {
        if (!needsReorderingFields(fields, sorts)) {
            return fields;
        }

        final List<String> sortFields = sorts.stream()
                .filter(ValuesBucketOrdering::isGroupingSort)
                .map(SortSpec::field)
                .collect(Collectors.toList());

        return fields.stream()
                .sorted(new FieldsSortingComparator(sortFields))
                .collect(Collectors.toList());
    }
@Test
    void pivotUsedForSortIsPulledToTop() {
        final List<SortSpec> pivotSorts = List.of(PivotSort.create("baz", SortSpec.Direction.Descending));

        final List<String> orderedBuckets = ValuesBucketOrdering.orderFields(List.of("foo", "bar", "baz"), pivotSorts);

        assertThat(orderedBuckets).containsExactly("baz", "foo", "bar");
    }