In trying to get a list of all music files from a particular album using mdfind I see that the files aren't necessarily returned in any order. I'd prefer album/track order if possible, but can't find a way to provide the sort order on the command line. The current invocation is something like:
mdfind -onlyin "$MUSICROOT" -literal "$MDQUERY"
where:
$MUSICROOT is ~/Music/iTunes
$MDQUERY is "kMDItemContentTypeTree == 'public.audio' && kMDItemAlbum == '*$1*'c"
Ideally I'd want to add something like the following to the query:
ORDER BY kMDItemAlbum, kMDItemAudioTrackNumber
Best Answer
At the risk of stating the obvious, 'sort'.
e.g. mdfind -onlyin "$MUSICROOT" -literal "$MDQUERY" | sort
will sort the output alphabetically, giving you Artist, Title, Track. (it won't do the cool iTunes trick of ignoring 'The').
The sort command has lots of options, including sorting on particular fields if you want more control.
Edit: The below shell script function will provide the desired sort in the specific case mentioned: