public Schema sorted() {
    // Create a new schema and copy over the appropriate Schema object attributes:
    // {fields, uuid, options}
    // Note: encoding positions are not copied over because generally they should align with the
    // ordering of field indices. Otherwise, problems may occur when encoding/decoding Rows of
    // this schema.
    Schema sortedSchema =
        this.fields.stream()
            .sorted(Comparator.comparing(Field::getName))
            .map(
                field -> {
                  FieldType innerType = field.getType();
                  if (innerType.getRowSchema() != null) {
                    Schema innerSortedSchema = innerType.getRowSchema().sorted();
                    innerType = innerType.toBuilder().setRowSchema(innerSortedSchema).build();
                    return field.toBuilder().setType(innerType).build();
                  }
                  return field;
                })
            .collect(Schema.toSchema())
            .withOptions(getOptions());
    sortedSchema.setUUID(getUUID());

    return sortedSchema;
  }
@Test
  public void testSorted() {
    Options testOptions =
        Options.builder()
            .setOption("test_str_option", FieldType.STRING, "test_str")
            .setOption("test_bool_option", FieldType.BOOLEAN, true)
            .build();

    Schema unorderedSchema =
        Schema.builder()
            .addStringField("d")
            .addInt32Field("c")
            .addStringField("b")
            .addByteField("a")
            .build()
            .withOptions(testOptions);

    Schema unorderedSchemaAfterSorting = unorderedSchema.sorted();

    Schema sortedSchema =
        Schema.builder()
            .addByteField("a")
            .addStringField("b")
            .addInt32Field("c")
            .addStringField("d")
            .build()
            .withOptions(testOptions);

    assertEquals(true, unorderedSchema.equivalent(unorderedSchemaAfterSorting));
    assertEquals(
        true,
        Objects.equals(unorderedSchemaAfterSorting.getFields(), sortedSchema.getFields())
            && Objects.equals(unorderedSchemaAfterSorting.getOptions(), sortedSchema.getOptions())
            && Objects.equals(
                unorderedSchemaAfterSorting.getEncodingPositions(),
                sortedSchema.getEncodingPositions()));
  }