# How to make a better Sort in my case

I have some file names like this:

fileNames = {"F:\\mathematica\\liu3\\62.jpg",
"F:\\mathematica\\liu3\\1.jpg", "F:\\mathematica\\liu3\\2.jpg",
"F:\\mathematica\\liu3\\3.jpg", "F:\\mathematica\\liu3\\4.jpg",
"F:\\mathematica\\liu3\\5.jpg", "F:\\mathematica\\liu3\\6.jpg",
"F:\\mathematica\\liu3\\7.jpg", "F:\\mathematica\\liu3\\42.jpg",
"F:\\mathematica\\liu3\\52.jpg"};


I want get a better order for they.But when I use Sort for this

Sort@fileNames


Then I get:

{"F:\mathematica\liu3\1.jpg", "F:\mathematica\liu3\2.jpg", \ "F:\mathematica\liu3\3.jpg", "F:\mathematica\liu3\42.jpg", \ "F:\mathematica\liu3\4.jpg", "F:\mathematica\liu3\52.jpg", \ "F:\mathematica\liu3\5.jpg", "F:\mathematica\liu3\62.jpg", \ "F:\mathematica\liu3\6.jpg", "F:\mathematica\liu3\7.jpg"}

But actually this ordering is my expectation

So how to get this list with my ordering by a terse method?

-
You are getting the same sort order as your OS give you, i.e., lexical order. The easiest way to fix this is not in Mathematica, but in your file system. Rename flies such as 3.jpg to 03.jpg or to 30.jpg, whichever way seem more useful to you. – m_goldberg Mar 25 at 5:40
Thanks for you advices.1)I have try some order in my Window 10,but have no my expected order,and I have not found that order you mean like lexical order.2)The original name is 3.jpg,4.jpg or 42.jpg.to add that 0 is little troublesome.3)I wanna sort it in mma because of I wannt import it into mma then convenient to following processing – yode Mar 25 at 5:52
Your screenshot looks quite strange, where's Name(名称)? And I can't find fonts related options in my OS, I'm using Win10, too. – xzczd Mar 25 at 5:59
@xzczd Click the More(更多). – yode Mar 25 at 6:09
You should check 排序方式, not 分组依据 囧 – xzczd Mar 25 at 8:11

SortBy[#, FileBaseName] &@fileNames

-
brilliant work ! +1 – Ali Hashmi Mar 25 at 10:47
I just noticed @xzczd's comment, which I agree with, that your file naming convention is not clear. For instance, does "123" mean the 23rd in the 1st group, or 3rd in 12th group? Better solution would be figured out were more clear naming convention provided. – sunt05 Mar 25 at 11:24
Yep,I'm aware of that confict.So I have already deleted it.Thank for your help. :) – yode Mar 25 at 11:28
Interesting, I didn't know "." doesn't take part in sorting. – xzczd Mar 25 at 11:42

#[[Ordering[StringTake[FileBaseName@# <> "0", 2] & /@ #]]] &@fileNames

-
As you promption of FileBaseName,then I get another try like fileNames[[Ordering[StringPadRight[FileBaseName/@fileNames,Automatic,"0"]]]],T‌​hank you very much. – yode Mar 25 at 6:07
@yode Oh, so there're functions for string padding in v10. (I'm still in v9. ) – xzczd Mar 25 at 8:21
I don' t know whether or not appropriate I update this post. – yode Mar 25 at 8:46
@yode Then since the serial number isn't limited to 0~9, your naming rule turns out to be unclear. For example, should "42" be taken as "42, 1st" or "4, 2nd"? – xzczd Mar 25 at 9:05
Seems to be little unclear,But in my casy "42" mean "4, 2nd" – yode Mar 25 at 9:19

there is a nasty way of doing this:

orig = (Riffle[ToCharacterCode[fileNames], (Characters /@ fileNames)]) //
Partition[#, 2] & // Thread[#] & /@ # & // #[[All, {21, 22}]] &

(*{{{54, "6"}, {50, "2"}}, {{49, "1"}, {46, "."}}, {{50, "2"}, {46,"."}},
{{51, "3"}, {46, "."}}, {{52, "4"}, {46, "."}}, {{53,"5"}, {46, "."}},
{{54, "6"}, {46, "."}}, {{55, "7"}, {46,"."}}, {{52, "4"}, {50, "2"}},
{{53, "5"}, {50, "2"}}} *)

sorted = Flatten[Position[orig, #] &/@ Sort@orig]

(* {2, 3, 4, 5, 9, 6, 10, 7, 1, 8} *)

fileNames[[sorted]]

(* {"F:\\mathematica\\liu3\\1.jpg", "F:\\mathematica\\liu3\\2.jpg",
"F:\\mathematica\\liu3\\3.jpg", "F:\\mathematica\\liu3\\4.jpg",
"F:\\mathematica\\liu3\\42.jpg", "F:\\mathematica\\liu3\\5.jpg",
"F:\\mathematica\\liu3\\52.jpg", "F:\\mathematica\\liu3\\6.jpg",
"F:\\mathematica\\liu3\\62.jpg", "F:\\mathematica\\liu3\\7.jpg"} *)

-
Thanks for you effort.:) – yode Mar 25 at 11:29
you are welcome ! – Ali Hashmi Mar 25 at 11:37