static struct option *options__order(const struct option *opts)
 {
-       int nr_opts = 0, len;
+       int nr_opts = 0, nr_group = 0, len;
        const struct option *o = opts;
-       struct option *ordered;
+       struct option *opt, *ordered, *group;
 
        for (o = opts; o->type != OPTION_END; o++)
                ++nr_opts;
                goto out;
        memcpy(ordered, opts, len);
 
-       qsort(ordered, nr_opts, sizeof(*o), option__cmp);
+       /* sort each option group individually */
+       for (opt = group = ordered; opt->type != OPTION_END; opt++) {
+               if (opt->type == OPTION_GROUP) {
+                       qsort(group, nr_group, sizeof(*opt), option__cmp);
+                       group = opt + 1;
+                       nr_group = 0;
+                       continue;
+               }
+               nr_group++;
+       }
+       qsort(group, nr_group, sizeof(*opt), option__cmp);
+
 out:
        return ordered;
 }