Question: I would like to sort the superscripts of the following mwe alphabetically. Automatically.

```
\documentclass{article}

\usepackage[utf8]{inputenc}

\newcommand{\x}[1]{\textsuperscript{#1}}

\newcommand{\red}{R}
\newcommand{\blu}{B}
\newcommand{\gre}{G}

\parindent0pt

\begin{document}
\section{the order I will use}
    test 1\x{\red\gre} \\
    test 2\x{\blu\red} \\
    test 3\x{\gre\blu}
\section{the order I should use/what should be displayed}
    test 1\x{\gre\red} \\
    test 2\x{\blu\red} \\
    test 3\x{\blu\gre}
\end{document}

```

Surely this can be easily done. But I never really started 'programing' within latex or whatever this might be called. Any help will be appreciated please ask if any clarification is needed.

[![enter image description here](https://i.stack.imgur.com/KMp6w.png)](https://i.stack.imgur.com/KMp6w.png)

Answer: 
I dumbed down the alpha-bubble sorter at [Trying to eliminate stack overflow during recursion](https://tex.stackexchange.com/questions/372639/trying-to-eliminate-stack-overflow-during-recursion) so that it would handle non-comma-separated, 1-character data.

LIMITATIONS: this uses ascii code of each token to sort the data, so that capital `Z` comes before lowercase `a`.

I show how to generically use the sorter at the end of the MWE.

```
\documentclass{article}

\usepackage[utf8]{inputenc}

\newcommand{\x}[1]{\edef\tmp{#1}\alphabubblesort{\tmp}\textsuperscript{\sortedlist}}
\newcommand{\red}{R}
\newcommand{\blu}{B}
\newcommand{\gre}{G}

\parindent0pt

\newcommand\alphabubblesort[1]{\def\sortedlist{}\expandafter\sortlist#1\relax\relax}
\def\sortlist#1#2#3\relax{%
  \let\next\relax
  \ifx\relax#2\relax%
    \edef\sortedlist{\sortedlist#1}%
  \else
    \ifnum`#1<`#2\relax%
      \edef\sortedlist{\sortedlist#1}%
      \def\next{\sortlist#2#3\relax}%
    \else%
      \ifnum`#1>`#2\relax%
        \let\tmp\sortedlist%
        \def\sortedlist{}%
        \def\next{\expandafter\sortlist\tmp#2#1#3\relax}%
      \fi%
    \fi%
  \fi%
\next
}
\begin{document}
\section{the order I will use}
    test 1\x{\red\gre} \\
    test 2\x{\blu\red} \\
    test 3\x{\gre\blu}
\section{the order I should use/what should be displayed}
    test 1\x{\gre\red} \\
    test 2\x{\blu\red} \\
    test 3\x{\blu\gre}

\textbf{To use just the sorter:}

\def\mydata{vkp!s45J[YO}
ASCII sorted data for \mydata{} is obtained simply with
\alphabubblesort{\mydata}\sortedlist.
\end{document}

```

[![enter image description here](https://i.stack.imgur.com/ASKEQ.jpg)](https://i.stack.imgur.com/ASKEQ.jpg)
Regarding the answer of Steven B. Segletes I expanded the question looking for a nested sorting function. This should be able to sort the macros of type X given in `\sortedNormal` as well as the macros of type Y given in `\sortedSuperscript` within those first macros of type X.

Writing this answer I realize, that this might be too much, especially when the macros of type X should be separated by comma. Which means I have to write to different `sortedList`s anyway, since macros of type Y should not be separated by comma. 

So if there is a solution I appreciate the help, but maybe this gets out of hand.

```
\documentclass{article}

\usepackage[utf8]{inputenc}

\newcommand{\sortedSuperscript}[1]{\edef\tmp{#1}\alphabubblesort{\tmp}\textsuperscript{\sortedlist}}
\newcommand{\sortedNormal}[1]{\edef\tmp{#1}\alphabubblesort{\tmp}\sortedlist}

\newcommand{\x}[1]{\sortedSuperscript{#1}}

  %% macros of type X
    \newcommand{\listA}{ColorlistA\x{\red\blu}}
    \newcommand{\listB}{ColorlistB\x{\gre\blu}}
    \newcommand{\listC}{ColorlistC\x{\red\gre}}
  %% macros of type Y
    \newcommand{\red}{R}
    \newcommand{\blu}{B}
    \newcommand{\gre}{G}

\parindent0pt

\newcommand\alphabubblesort[1]{\def\sortedlist{}\expandafter\sortlist#1\relax\relax}
\def\sortlist#1#2#3\relax{%
  \let\next\relax
  \ifx\relax#2\relax%
    \edef\sortedlist{\sortedlist#1}%
  \else
    \ifnum`#1<`#2\relax%
      \edef\sortedlist{\sortedlist#1}%
      \def\next{\sortlist#2#3\relax}%
    \else%
      \ifnum`#1>`#2\relax%
        \let\tmp\sortedlist%
        \def\sortedlist{}%
        \def\next{\expandafter\sortlist\tmp#2#1#3\relax}%
      \fi%
    \fi%
  \fi%
\next
}

\begin{document}
    \section{expected result hard coded}
        ColorlistA\textsuperscript{BR}, ColorlistB\textsuperscript{BG}, ColorlistC\textsuperscript{RG}

    \section{expected result with macros}
        \sortedNormal{\listC\listA\listB}

\end{document}

```