0

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.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
David
  • 33
  • 3

1 Answers1

2
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];
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • 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 – David Feb 13 '16 at 17:07
  • @David I have added something that might work. – Chris Degnen 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 – David Feb 13 '16 at 17:31