-2

I have a list of files each named after a camera. In the Finder on a Mac, if I sort them by "Name" the first three are in this order:

1D 5D 10D

However, in another application, the same list of files is sorted like this:

10D 1D 5D

Notice the 10D moved from the bottom to the top of the list. I think this is because the the second app is favoring numbers over letters. It's like because "0" is a number it has a higher placement than "D".

In Python, I have written a script that accepts a folder of files and adds them to a list. But I need the list to be sorted the same way the second app sorts them, not the way the Finder sorts them. I think the solution might have to do with the sorted() function and the key parameter but beyond that I'm not sure what to do.

For those of you who want to see code:

myList = ["1D", "5D", "10D"]
sorted(myList, key=theSolutionToMyProblem)
print myList
>>>["10D", "1D", "5D"]

If I'm way off please tell me. Point me in the right direction.

wetjosh
  • 6,288
  • 4
  • 23
  • 32
  • 1
    Related: http://stackoverflow.com/q/442429 – Robert Harvey Sep 19 '14 at 19:37
  • I don't think this is just related, it's exactly the same question, except referring to Finder's natural sort order instead of Explorer's. – abarnert Sep 19 '14 at 19:41
  • Unless you actually want _exactly_ what Finder does, on the specific computer you're running on, down to the smallest quirk, not just natural sorting in general… in which case I can write an answer using `PyObjC` to call the same CoreFoundation function the Finder uses. – abarnert Sep 19 '14 at 19:42
  • 1
    @abarnert, OP isn't asking why Finder sorts the way it does, OP is asking how to sort it as the second application does – wnnmaw Sep 19 '14 at 19:44
  • 1
    @wnnmaw: I know he isn't asking _why_ Finder sorts it that way… but I thought he was asking _how to do the same thing_. But apparently even that is wrong, in which case this isn't a dup, just a desperately silly question. – abarnert Sep 19 '14 at 19:53
  • OK, I get why he's confused now, and it's not quite as silly. He _is_ sorting the values the way he wants… and then ignoring the result and printing out the original, unsorted list, which just happens to be in natural sort order. – abarnert Sep 19 '14 at 19:57
  • This was a horrible question. I admit it. Everyone should down vote it. I deserve to lose points. Is there a charity I can donate my points to? How can I be forgiven of this programming sin? I didn't try. I just didn't try :( – wetjosh Sep 19 '14 at 20:55
  • @wetjosh: The dumbest mistakes are always the ones you can't find yourself, so you end up asking other people and then feeling like an idiot. That's how it goes. I once spent an entire wondering why my gzip-compressed JSON wasn't parsing at the other end which re-compressed the data instead of decompressing it, looking for stray braces and single quotes and so on before realizing that hold on, the actual input to `json.loads` is random-looking garbage, not the stuff I'm looking at. – abarnert Sep 19 '14 at 23:58

2 Answers2

2

As wnnmaw points out, you're asking how to sort things in the default sort order for Python.

I think your actual confusion here is that you're not actually looking at the sorted results:

myList = ["1D", "5D", "10D"]
sorted(myList, key=theSolutionToMyProblem)
print myList
>>>["10D", "1D", "5D"]

No matter what you use for theSolutionToMyProblem isn't going to make any difference.

sorted makes a sorted copy of myList and returns it, without affecting myList itself in any way. You ignore that sorted copy, and print out myList.

So, you need to do either this:

mySortedList = sorted(myList)
print mySortedList

… or:

myList.sort()
abarnert
  • 354,177
  • 51
  • 601
  • 671
1

The default sorted() (or list.sort()) return value is what the second app returns

>>> lst = ["1D", "5D", "10D"]
>>> lst.sort()
>>> lst
['10D', '1D', '5D']
wnnmaw
  • 5,444
  • 3
  • 38
  • 63