To achieve this there are several options, and the best choice depends on your compiler and your operating system. Most solutions I've found on the other sites place the different pieces of text (be it sections, subsections or macros) in separate files with a their filename as the sort key.
Then you have a number of options to include the files in the correct order (this list is probably not exhaustive):
- Manually sort the list of
\input
commands, which is at least easier than cut-and-paste whole blocks of text
- Use a shell script to list files in a folder, sort them and write the list of files as
\input
commands to a tex file that is then statically included in your main file.
- Use LuaTeX to read the folder contents and write the
\input
commands directly in the main file.
- Another option using Lua is to use a macro to store all sections in a key/value array, sort the array by the keys and then write the contents in sorted order in the output.
Since the options are quite numerous and each one is a custom tailored solution I did not create an example of either, but this should help you investigate your options. If you could give more details about your setup or preferences I might be able to help you further.
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit