From: Chet Ramey
Subject: Re: feature proposal: expand associative arrays in lexicographic order
of keys
Date: Mon, 24 Aug 2026 17:00:16 -0400
>I'm actually struggling to find where the devel branch's doc/bash.1
>bothers to say that the expansion of indexed arrays subscripted by @
>or * is in numerical order of indices and the expansion of associative
>arrays subscripted by @ or * is in arbitrary order.
>It doesn't specify expansion order at all.
I think there should be something in the docs about this.
Just a simple statement that:
a) Indexed arrays are returned in index (numeric) order.
and
b) Associative arrays are returned in arbitrary (i.e., no) order.
>It makes the most sense to expand indexed arrays in ascending order of
>indices, to sort of mirror C arrays.
Agreed. Note that if this were not true (i.e., if indexed arrays were
returned in arbitrary order), then lots of things would not work as
expected. In particular, I have a script that:
a) Copies "$@" to an (indexed) array.
b) Manipulates that array (modifying elements and/or removing them)
c) Passes that array to an external command (mimicking the usual:
cmd "$@"
idiom).
This would fall apart if the array weren't returned in numerical order.
>It doesn't make sense to impose some kind of ordering on associative
>arrays, which are essentially dicts/hash tables.
This is something upon which reasoned men can disagree. I have long
argued in the GAWK group(s) that GAWK *should* return arrays in sorted
order, primarily because another AWK implementation with which I am
familiar does and it turns out to be quite useful. I was met with the
expected resistance from folks who maintained that "No, associative
arrays are just hash tables; they have no order". Of course, this was
all before GAWK did (eventually) implement array sorting (in a few
different flavors, as it turns out).
>>When expansion order matters at all, this is liable to be what the
>>shell programmer would prefer. ${!assoc[@]} expanding in lexicographic
>>order would make for a slight improvement in one of my scripts, at
>>least.
>This is not a feature that has been in great demand (or any demand,
>really), especially not sorting in lexicographic order for keys.
I'm surprised at this. It is frequently useful and not at all outre.
>If you want to wait for the next devel branch push, I added a -I option
>to the `asort' loadable builtin to sort an associative array by keys
>and assign those keys in order to an indexed array. (The existing -i
>option is like awk's asorti, which sorts by values and then assigns the
----------------^^^
>keys, so it was only a small change.) See if that does what you want
>without any other changes.
FWIW, I could not figure out how to use the bash "asort" extension.
The idiom that I use is like:
mapfile -t < <(for i in "${!MyArray[@]}"; do echo "$i";done | sort)
select pick in "${MAPFILE[@]}"; do ...; done
P.S. Above, indicated by the "----------------^^^", I think you
should have referred to it as "GAWK's asorti()", since the feature is
GAWK-specific (not generic AWK).
=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.
When responding to my posts, please try to refrain from giving bureaucratic
answers. If you have nothing useful to say, then just click Next and go on.
BTW, ain't interested in kludgey workarounds - got enough of my own!
(YT id: 6H1lcubN0oE)