this is an extended question taken by Alphabetically display the items in itemize
The solution posted by Werner is amazing. But what I need is to sort by a value of a macro.
For example \foo{Label} is a function that take Label as argument and give you SortLabel as string.
\foo{Label1} %Will print SortLabel1
\foo{Label2} %Will print SortLabel2
\foo{Label3} %Will print SortLabel3
\begin{sortedlist}
\sortitem[\foo{Label1}]{Bar1}
\sortitem[\foo{Label2}]{Bar2}
\sortitem[\foo{Label3}]{Bar3}
\end{sortedlist}
Tryed to use \expandafter, but seems that datatool can store only by \foo{Label1} and can't sort for SortLabel1....
To be more specific, the function \foo for me is \glossentryname{} that give me the name of a glossary entry. Maybe this could be the reason that expansion doesn't work. Here's a MnWE
\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\usepackage{glossaries, glossary-tree}
\makeglossaries
%%%%Sorted Functions%%%%
\newcommand{\sortitem}[1]{%
\DTLnewrow{list}% Create a new entry
\def\tmp{\DTLnewdbentry{list}{description}}%
\expandafter\tmp\expandafter{#1}% Add entry as description
}
\newenvironment{sortedlist}{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
\DTLsort{description}{list}% Sort list
\begin{itemize}%
\DTLforeach*{list}{\theDesc=description}{%
\item \theDesc}% Print each item
\end{itemize}%
}
%%%The Glossary Part%%%%%
\longnewglossaryentry{MVARglossary}{name=MVAR}{
The Description of glossary (not useful at the moment)
}
\begin{document}
Default:
\begin{itemize}
\item ISDYNSTP: Is dynamic time step used ?
\item ISCDCA:
\item MVAR
\item IS2TL
\end{itemize}
\def\foo{ISDYNSTP: Is dynamic time step used ?}
\def\bar{\glossaryname{MVARglossary}}
\glossentryname{MVARglossary}
\bar
Sorted:
\begin{sortedlist}
\sortitem{\foo}
\sortitem{ISCDCA:}
\sortitem{\bar}
\sortitem{\glossentryname{MVARglossary}}
\sortitem{IS2TL}
\end{sortedlist}
\end{document}
suggests?

\glossentrynameisn't expandable, so it's going to be hard to sort\glossentryname{MVARglossary}. – Nicola Talbot May 04 '16 at 14:22\gls{}? – Alberto Verdoja May 04 '16 at 14:26\glsentryfieldname commands. (Not the case changing variants like\Glsentrynameor\glsentrytitlecase, just the ones like\glsentrynameor\glsentrytext.) – Nicola Talbot May 04 '16 at 17:48