%%%
% Rangement des nombres
%%%
\def\filedateRgtNb{2025/11/01}%
\def\fileversionRgtNb{0.1b}%
\message{-- \filedateRgtNb\space v\fileversionRgtNb}%
%
\setKVdefault[ClesRgt]{Decroissant=false,Strict,Fraction=false,Details=false,Seul=false}%

\ExplSyntaxOn
\NewDocumentCommand\RangementListe{sm}{%
  \clist_clear:N \l_Rgt_Tampon
  \clist_set:Ne \l_Rgt_Tampon {#2}
  \IfBooleanF{#1}{\clist_remove_duplicates:N \l_Rgt_Tampon}
  \clist_sort:Nn \l_Rgt_Tampon
  {
    \fp_compare:nNnTF { ##1 } > { ##2 }
    { \sort_return_swapped: }
    { \sort_return_same: }
  }
  \edef\PfCRetiensRangement{\clist_use:N \l_Rgt_Tampon}%
}
\ExplSyntaxOff

\NewDocumentCommand\Rangement{som}{%
  \useKVdefault[ClesRgt]%
  \setKV[ClesRgt]{#2}%
  \setsepchar{,}\ignoreemptyitems%
  \readlist*\ListeRgt{#3}%
  \reademptyitems%
  \ifboolKV[ClesRgt]{Fraction}{%
    \setsepchar[*]{,*/}\ignoreemptyitems%
    \readlist*\ListeRgt{#3}%
    % on cherche le d\'enominateur commun
    \ppcm=1\relax%
    \foreachitem\x\in\ListeRgt{%
      \PPCM{\fpeval{\ListeRgt[\xcnt,2]}}{\fpeval{\the\ppcm}}%
    }%
    % 
    \xdef\FooFraction{\fpeval{\ListeRgt[1,1]*\the\ppcm/\ListeRgt[1,2]}}%
    \xintFor* ##1 in{\xintSeq{2}{\ListeRgtlen}}\do{%
      \xdef\FooFraction{\FooFraction,\fpeval{\ListeRgt[##1,1]*\the\ppcm/\ListeRgt[##1,2]}}%
    }%
    \RangementListe{\FooFraction}%
    \readlist*\PfCListeFinaleRgt{\PfCRetiensRangement}%
    %%%%%%%%% 
    \ifboolKV[ClesRgt]{Decroissant}{%
      \ifboolKV[ClesRgt]{Details}{%
        \ensuremath{%
          \xintFor* ##1 in{\xintSeq{0}{\fpeval{\PfCListeFinaleRgtlen-1}}}\do{%
            \xintifForFirst{}{\ifboolKV[ClesRgt]{Strict}{>}{\geqslant}}\frac{\num{\PfCListeFinaleRgt[\fpeval{\PfCListeFinaleRgtlen-##1}]}}{\num{\the\ppcm}}
          }%
        }%
      }{%
        \ensuremath{%
          \xintFor* ##1 in{\xintSeq{0}{\fpeval{\PfCListeFinaleRgtlen-1}}}\do{%
            \xintifForFirst{}{\ifboolKV[ClesRgt]{Strict}{>}{\qeqslant}}\Simplification{\PfCListeFinaleRgt[\fpeval{\PfCListeFinaleRgtlen-##1}]}{\the\ppcm}}%
        }%
      }%
    }{%
      \ifboolKV[ClesRgt]{Details}{%
        \ensuremath{%
           \xintFor* ##1 in{\xintSeq{1}{\PfCListeFinaleRgtlen}}\do{%
             \xintifForFirst{}{\ifboolKV[ClesRgt]{Strict}{<}{\leqslant}}\frac{\num{\PfCListeFinaleRgt[##1]}}{\num{\the\ppcm}}%
           }%
        }%
      }{%
        \ensuremath{%
          \xintFor* ##1 in{\xintSeq{1}{\PfCListeFinaleRgtlen}}\do{%
            \xintifForFirst{}{\ifboolKV[ClesRgt]{Strict}{<}{\leqslant}}\Simplification{\PfCListeFinaleRgt[##1]}{\the\ppcm}%
          }%
        }%
      }%
    }%
  }{%
    \IfBooleanTF{#1}{\RangementListe*{#3}}{\RangementListe{#3}}%
    % 
    \ifboolKV[ClesRgt]{Seul}{}{%
      \readlist*\PfCListeFinaleRgt{\PfCRetiensRangement}%
      \ifboolKV[ClesRgt]{Decroissant}{%
        \ensuremath{%
          \xintFor* ##1 in{\xintSeq{0}{\fpeval{\PfCListeFinaleRgtlen-1}}}\do{%
            \xintifForFirst{}{\ifboolKV[ClesRgt]{Strict}{>}{\geqslant}}\num{\PfCListeFinaleRgt[\fpeval{\PfCListeFinaleRgtlen-##1}]}%
          }%
        }%
      }{%
        \ensuremath{%
          \xintFor* ##1 in{\xintSeq{1}{\fpeval{\PfCListeFinaleRgtlen}}}\do{%
            \xintifForFirst{}{\ifboolKV[ClesRgt]{Strict}{<}{\leqslant}}\num{\PfCListeFinaleRgt[##1]}%
          }%
        }%
      }%
    }%
  }%
  \reademptyitems%
}%