# Can't sort my list numerically for larger numbers than 10 e.g. 1,10,100,2 [duplicate]

I want my files names in real numerical order so 1,2,3,10,12. However it is currently putting them in order like so: 1,10,12,2,3

Input:

files = FileNames["*"]

SortBy [files, N]


Output:

{"10", "1007680", "1007690", "1007700", "1007710", "1007720", \
"1007730", "1007740", "1032540", "1032550", "1032560", "1032570", \
"1032580", "1032590", "1032600", "1032610", "11250", "11260", \
"11280", "11300", "134110", "134120", "134130", "134140", "134150", \
"20", "30", "301720", "301730", "301740", "301750", "301760", \
"38560", "38570", "38580", "38590", "38600", "40", "50", "60", \
"662830", "662840", "662850", "662860", "662870", "662880"}


There must be a simple way to do this that i have missed!

My main goal is that I want mathematica to do a set of operations on each file and plot the results in order of the file name. E.g. so that I can see a change from file 1 to file 1000000.

So my thinking is that I want the list to be like this:

{"10", "20", "30", "40", "50", "60", "3200", "5600", "10000" ...}


Then Mathematica will process and plot the files in the correct order.

• That worked thank you! Why is it that SortBy could not do this? I'm still fairly new at this. However mathematica does not appear to be plotting the operations i do on my files in the correct order Feb 13 '16 at 16:48
• SortBy[a, ToExpression] would do it. Feb 13 '16 at 16:51
• The styling of your question can be improved. Please take the time to read How to copy code from Mathematica so it looks good on this site and Hints and help on using the site efficiently and How do I format my posts using Markdown or HTML? and read the FAQ.
– user9660
Feb 13 '16 at 16:52
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Feb 13 '16 at 16:54
• Possible duplicate of 764 and also see 3248.
– gwr
Feb 13 '16 at 17:00

a = {"10", "1007680", "1007690", "1007700", "1007710", "1007720",
"1007730", "1007740", "1032540", "1032550", "1032560",
"1032570", "1032580", "1032590", "1032600", "1032610",
"11250", "11260", "11280", "11300", "134110", "134120",
"134130", "134140", "134150", "20", "30", "301720", "301730",
"301740", "301750", "301760", "38560", "38570", "38580",
"38590", "38600", "40", "50", "60", "662830", "662840",
"662850", "662860", "662870", "662880"};

ToString /@ Sort[ToExpression[a]]


or

SortBy[a, ToExpression]


Edit further to comment.

SetDirectory["Documents"];
filelist = FileNames[];
files = SortBy[filelist, ToExpression];
alldata = Map[Import[#, "TSV"] &, files];

• Thank you! I have realised i need to import the files in this order and i'm not sure how to do that my function is: alldata = Table[Import[files[[i]], "TSV"], {i, 1, L}] and this is importing them in the original order which is not what i want Feb 13 '16 at 17:07
• @David I have added something that might work. Feb 13 '16 at 17:16
• Thank you, it did work however i think it has cause my other operations to not work. What i think i will do is post a new question with all my code and a better explanation Feb 13 '16 at 17:31