0
$\begingroup$

This question already has an answer here:

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.

$\endgroup$

marked as duplicate by MarcoB, user9660, m_goldberg, Kuba, Bob Hanlon Feb 14 '16 at 15:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ 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 $\endgroup$ – David Feb 13 '16 at 16:48
  • 2
    $\begingroup$ SortBy[a, ToExpression] would do it. $\endgroup$ – Chris Degnen Feb 13 '16 at 16:51
  • $\begingroup$ 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. $\endgroup$ – user9660 Feb 13 '16 at 16:52
  • $\begingroup$ 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! $\endgroup$ – user9660 Feb 13 '16 at 16:54
  • 5
    $\begingroup$ Possible duplicate of 764 and also see 3248. $\endgroup$ – gwr Feb 13 '16 at 17:00
2
$\begingroup$
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];
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – David Feb 13 '16 at 17:07
  • $\begingroup$ @David I have added something that might work. $\endgroup$ – Chris Degnen Feb 13 '16 at 17:16
  • $\begingroup$ 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 $\endgroup$ – David Feb 13 '16 at 17:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.