code: public static void getOrdering(int p, int n, List<String> list, int[] gt, int pos, int[] map) {
          for (int a = 0; a <= n; a++) {
              if (map == null) {
                  gt[pos] = a;
              } else {
                  // Remap allele
                  gt[pos] = remapAllele(map, a);
              }
              if (p == 1) {
                  int prev = gt[0];
                  int[] gtSorted = gt;
                  // Check if sorted
                  for (int g : gt) {
                      if (prev > g) {
                          // Sort if needed. Do not modify original array
                          gtSorted = Arrays.copyOf(gt, gt.length);
                          Arrays.sort(gtSorted);
                          break;
                      }
                  }
                  StringBuilder sb = new StringBuilder();
                  for (int g : gtSorted) {
                      sb.append(g);
                  }
                  list.add(sb.toString());
              } else {
                  getOrdering(p - 1, a, list, gt, pos - 1, map);
              }
          }
      }
docs: Recursive genotype ordering algorithm as seen in VCF-spec.
  
  @link https://samtools.github.io/hts-specs/VCFv4.3.pdf
  
  @param p     ploidy
  @param n     Number of alternates
  @param list  List where to store the genotypes
  @param gt    Shared array
  @param pos   Position where to write in the array
  @param map   Map from reordered allele position to original allele position