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

Feature request: two level sorting



Hello
Suppose you have set of file names:

files=( "aaa-A" "aab-B" "aac-A" "aad-C" )

when sorted normally, it will yield:

# print -rl -- ${(o)files[@]}
aaa-A
aab-B
aac-A
aad-C

when sorted with grouping on A, this will be:

aaa-A
aac-A
aab-B
aad-C

**The thing is** that it is easy to provide group names in separate array:

# groups=( "${files[@]//(#b)*([A-Z])/$match[1]}" )
# print -rl -- "${groups[@]}"
A
B
A
C

With that in place, one can sort with the grouping in following way:

    files=( "${(o)files[@]}" )
    group_letters=( A B C )
    integer a i grsize="${#group_letters}" size="${#files}"
    out=( )
    for (( a=1; a<=grsize; a++ )); do
        selected_group="${group_letters[a]}"
        for (( i=1; i<=size; i++ )); do
            [ "$selected_group" != "${groups[i]}" ] && continue
            out+=( "${files[i]}" )
        done
    done
    print -rl "${out[@]}"

So this is somewhat an amount of code. The group-sort flag could take
group names of sorted data in additional parameter, e.g.:
"${(ox:group_letters:)files}"

Best regards,
Sebastian Gniazdowski



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