The best kittens, technology, and video games blog in the world.

Sunday, January 27, 2019

finder-sort

Cat (OOF!) by Long Road Photography (formerly Aff) from flickr (CC-NC-ND)

I was playing a bit with Electron, building a small image viewer, as Xee is weirdly crashy nowadays. A small aspect of any image viewer is sorting images to show them in order. As far as I know there's no programming language with any special support for sorting file names, and default ASCII sort is just atrocious - going cat1.jpg,  cat10.jpg,  cat2.jpg etc.

It's not uncommon to find file manager or image viewer which uses this completely unsuitable sort order.

OSX Finder is notable for very much not doing so, and using human-friendly ordering. Exact details are unfortunately not properly documented.

I created something which works more or less the same, and published it as npm package finder-sort.

There could be some differences, especially for non-ASCII locales. In the end, it's trying to solve the same problem, not necessarily match OSX Finder exactly.

The source code is on github.

About the only interesting thing about it from code point of view is that I used ava testing package this time. Javascript has over 9000 different testing packages, and it's not uncommon for a single program to duct tape together 10+ of them. I'm surprised there's no clear winner yet. Some of the new ones like ava and cypress seem mostly decent.

2 comments:

Anonymous said...

Foundation has a method -[NSString localizedStandardCompare:] which is documented as "Compares strings as sorted by the Finder". It does say that the exact sorting behavior is different for different locales and may be changed (and doesn't elaborate), which doesn't help for replicating the behavior, but it does mean that any language that can bridge to Obj-C can use this on macOS/iOS.

taw said...

Thanks for the pointer, I missed that. Actually bridging to ObjC would be painfully slow and not portable, but it would be useful to see a spec.

I'm not seeing any implementation or even good examples for localizedStandardCompare.