Having defined sage: x = SR.var('x', 20) sage: a = [x[11], x[8], x[10], x[9]]
sorting by repr or by str is disappointing: sage: sorted(a, key=repr) [x10, x11, x8, x9] sage: sorted(a, key=str) [x10, x11, x8, x9] As Dima suggests, one can ./sage --pip install natsort and then sage: from natsort import natsorted sage: natsorted(a) [x8, x9, x10, x11] Without any pip install, one can alternatively use `LooseVersion`: sage: from distutils.version import LooseVersion sage: sorted(a, key=lambda z: LooseVersion(str(z))) [x8, x9, x10, x11] Note: "./sage --pip install" requires Python's OpenSSL module to be built. If it was not built, install OpenSSL, - either system-wide with a package manager, for example under Debian or Ubuntu do sudo apt-get install openssl libssl-dev - or just for your Sage installation by running in a terminal from the Sage root directory: sage -i openssl sage -i pyopenssl Once OpenSSL is installed by one of the above methods, Sage's Python needs to be rebuilt by running the following in a terminal from the Sage root directory sage -f python2 && sage -f python3 && make Note also, if x and a were defined as above: sage: x = SR.var('x', 20) sage: a = [x[11], x[8], x[10], x[9]] then without any pip install or any import, one could also do: sage: sorted(a, key=lambda z: x.index(z)) [x8, x9, x10, x11] -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at https://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.