6
$\begingroup$

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

Mathematica graphics

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

$\endgroup$
8
  • 1
    $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented Mar 25, 2016 at 5:40
  • $\begingroup$ 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 $\endgroup$
    – yode
    Commented Mar 25, 2016 at 5:52
  • 1
    $\begingroup$ Your screenshot looks quite strange, where's Name(名称)? And I can't find fonts related options in my OS, I'm using Win10, too. $\endgroup$
    – xzczd
    Commented Mar 25, 2016 at 5:59
  • $\begingroup$ @xzczd Click the More(更多). $\endgroup$
    – yode
    Commented Mar 25, 2016 at 6:09
  • $\begingroup$ You should check 排序方式, not 分组依据 囧 $\endgroup$
    – xzczd
    Commented Mar 25, 2016 at 8:11

3 Answers 3

9
$\begingroup$

How about this one?:

SortBy[#, FileBaseName] &@fileNames
$\endgroup$
4
  • $\begingroup$ brilliant work ! +1 $\endgroup$
    – Ali Hashmi
    Commented Mar 25, 2016 at 10:47
  • $\begingroup$ 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. $\endgroup$
    – sunt05
    Commented Mar 25, 2016 at 11:24
  • $\begingroup$ Yep,I'm aware of that confict.So I have already deleted it.Thank for your help. :) $\endgroup$
    – yode
    Commented Mar 25, 2016 at 11:28
  • $\begingroup$ Interesting, I didn't know "." doesn't take part in sorting. $\endgroup$
    – xzczd
    Commented Mar 25, 2016 at 11:42
3
$\begingroup$

How about this?:

#[[Ordering[StringTake[FileBaseName@# <> "0", 2] & /@ #]]] &@fileNames
$\endgroup$
7
  • 1
    $\begingroup$ As you promption of FileBaseName,then I get another try like fileNames[[Ordering[StringPadRight[FileBaseName/@fileNames,Automatic,"0"]]]],Thank you very much. $\endgroup$
    – yode
    Commented Mar 25, 2016 at 6:07
  • $\begingroup$ @yode Oh, so there're functions for string padding in v10. (I'm still in v9. ) $\endgroup$
    – xzczd
    Commented Mar 25, 2016 at 8:21
  • $\begingroup$ I don' t know whether or not appropriate I update this post. $\endgroup$
    – yode
    Commented Mar 25, 2016 at 8:46
  • 1
    $\begingroup$ @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"? $\endgroup$
    – xzczd
    Commented Mar 25, 2016 at 9:05
  • $\begingroup$ Seems to be little unclear,But in my casy "42" mean "4, 2nd" $\endgroup$
    – yode
    Commented Mar 25, 2016 at 9:19
2
$\begingroup$

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"} *)
$\endgroup$
1
  • $\begingroup$ you are welcome ! $\endgroup$
    – Ali Hashmi
    Commented Mar 25, 2016 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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