List<Schema.Field> fields = Lists.newArrayList();
AvroType<S> parentAvroType = (AvroType<S>) ptype;
Schema parentAvroSchema = parentAvroType.getSchema();
for (int index = 0; index < orders.length; index++) {
ColumnOrder columnOrder = orders[index];
AvroType<?> atype = (AvroType<?>) ptype.getSubTypes().get(index);
Schema fieldSchema = atype.getSchema();
String fieldName = parentAvroSchema.getFields().get(index).name();
// Note: avro sorting of strings is inverted relative to how sorting works for WritableComparable
// Text instances: making this consistent
Schema.Field.Order order = columnOrder.order() == Order.DESCENDING ? Schema.Field.Order.DESCENDING :
Schema.Field.Order.ASCENDING;
fields.add(new Schema.Field(fieldName, fieldSchema, "", null, order));
}
schema.setFields(fields);
return schema;