# Alphabetize index subentries with references

I'm able to print an index alphabetically. However, if I use \ref in the index entry, the subentries are not in alphabetical order. I'd like Module 3 entry to come after Module 2 entry. How can I tell LaTeX to sort/alphabetize by the chapter number rather than chapter name?

Here's a MWE:

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{makeidx}
\makeindex

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Node Management} \label{chap:NodeMgmt}
Chapter text here.

\section{Learning Check}
\index{Learning Check!Module~\ref{chap:NodeMgmt}}
Question\\

Question\\

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{User Management} \label{chap:UserMgmt}
Chapter text here.

\section{Learning Check}
\index{Learning Check!Module~\ref{chap:UserMgmt}}
Question\\

Question\\
Question\\

%%%%%%%%%%%%%%%%%%%%
\chapter{Application Management} \label{chap:AppMgmt}
Chapter text here.

\section{Learning Check}
\index{Learning Check!Module \ref{chap:AppMgmt} @Module~\thechapter}
Question\\

Question\\

\printindex
\end{document}


The problem is that the \refs are not expanded when send to the .idx file. Therefore, the entries are not ordered by the numbers but by the content of \ref, the first one being \ref{chap:AppMgmt}, which will result in Module 3 in the end.

What must be done is to expand the reference before sending it to the .idx file. This can be done with \newcommand{\myindex}[1]{\expandafter\index\expandafter{#1}}. Then changing every \index for \myindex will give the desired output.

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{makeidx}
\makeindex

\newcommand{\myindex}[1]{\expandafter\index\expandafter{#1}}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Node Management} \label{chap:NodeMgmt}
Chapter text here.

\section{Learning Check}
\myindex{Learning Check!Module~\ref{chap:NodeMgmt}}
Question\\

Question\\

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{User Management} \label{chap:UserMgmt}
Chapter text here.

\section{Learning Check}
\myindex{Learning Check!Module~\ref{chap:UserMgmt}}
Question\\

Question\\
Question\\

%%%%%%%%%%%%%%%%%%%%
\chapter{Application Management} \label{chap:AppMgmt}
Chapter text here.

\section{Learning Check}
\myindex{Learning Check!Module~\ref{chap:AppMgmt}@Module~\thechapter}
Question\\