Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Feature request: two level sorting

On Thu, Jun 16, 2016 at 12:38 AM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Jun 15,  8:25pm, Mikael Magnusson wrote:
> }
> } Can we do what the oe:: flag does for globbing? Ie, pass each element
> } to a custom code snippet that transforms it arbitrarily, and sort on
> } the output.
> We considered that a while back, but the problem is that if you allow
> arbitrary shell code to be called from within the sort algorithm there
> are way too many opportunities for madness.
> With globbing, there's one well-defined place -- the read of the name
> from the directory structure -- where the callback can be applied to
> a single data element.  With a sort, the callback may be invoked many
> times for pairs of data, sometimes even more than once for the same
> pair depending on the algorithm, and unlike e.g. perl that has object
> pointers and reference-count garbage collection and lexical scopiing
> and a whole other panoply of protections against the user shooting
> himself, in zshell code all sorts of havoc could be wrought.  (Yeah,
> you can probably create the same havoc in perl if you work at it, but
> it's a lot more difficult.)

Ah, yeah I suppose that makes sense, we would have to add a bunch of
extra internal state to track the "sort-name" for each member during
expansion, which is very specific, if we wanted to avoid the multiple

> Falling back to something lesser/safer amounts to inventing a whole
> new mini-language.  It's just not worth it to internalize in the shell
> something that (a) other languages do better and (b) was intended by
> the original architecture to be done in an outside process anyway.

Well, this old trick always works if you don't mind depending on /
being readable, but that's probably a safe assumption usually.

% myarray=("aaa-A" "aab-B" "aac-A" "aad-C")
% echo /(e:'reply=($myarray)':oe:'REPLY=${REPLY#*-}':)
aac-A aaa-A aab-B aad-C

Mikael Magnusson

Messages sorted by: Reverse Date, Date, Thread, Author