Usually, counters in latex reference their parent counter when being initialised: \newcounter{NameOfTheNewCounter}[NameOfTheOtherCounter]
. This way, the \label
command can access all the parents in the tree structure and their numeric index values. Cleveref makes use of this fact, and notes down the entire structure when the \label
command is used. This is written into the .aux
file. So for Subsection 1.3.4 with label \label{aSubSection}
, this would be:
\newlabel{aSubSection@cref}{{[subsubsection][4][3,1]1.3.4}{1}}
So, when sorting, cleveref just compares the indicies 4, 3, 1
in reverse order. No lexicographical ordering at all - clever cleveref!
Now, you have stumbled on a bug in cleveref. Until a fix is published, here is a workaround:
\documentclass{article}
\usepackage[sort]{cleveref}
\makeatletter
\let\cref@old@resetby\cref@resetby%
\def\cref@resetby#1#2{
\let#2\relax%
\ifnum\pdfstrcmp{#1}{enumii}=\z@
\def#2{enumi}%
\fi%
\ifnum\pdfstrcmp{#1}{enumiii}=\z@
\def#2{enumii}%
\fi%
\ifnum\pdfstrcmp{#1}{enumiv}=\z@
\def#2{enumiii}%
\fi%
\ifnum\pdfstrcmp{#1}{enumv}=\z@
\def#2{enumiv}%
\fi%
\ifx#2\relax%
\cref@old@resetby{#1}{#2}
\fi}%
\makeatother
\begin{document}
\begin{enumerate}
\item foo
\begin{enumerate}
\item 1 \label{1}
\item 2 \label{2}
\begin{enumerate}
\item 5 \label{7}
\item 3 \label{4}
\end{enumerate}
\end{enumerate}
\item bar \label{6}
\begin{enumerate}
\item 3 \label{3}
\begin{enumerate}
\item 5 \label{5}
\end{enumerate}
\end{enumerate}
\end{enumerate}
The reference comes out as: \cref{1,2,3}, \cref{1,2} \cref{2,3}
and \cref{1,2,3,4,5,6,7}. As they should!
\end{document}
The bug is basically an incorrect string comparison function used (testing parameter values with \ifx) - comparing strings in latex can be so hard!
This gives the output:

cleveref
. You should contact the author of the package and send him your MWE.