Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9905

Re: Sorting with GNU Awk

From Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups comp.lang.awk
Subject Re: Sorting with GNU Awk
Date 2025-02-11 02:47 +0000
Organization A noiseless patient Spider
Message-ID <20250210184000.795@kylheku.com> (permalink)
References <voebnd$1fnqi$1@dont-email.me>

Show all headers | View raw


On 2025-02-11, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> I have an application were I need sorting. My tests led me to
> this variant that works as I expected (with GNU Awk 5.3.0)
>
>   asort (arr, res, "@val_num_desc")
>
> But I originally intended an _inplace_ sort and I thought that
> using these commands would also work
>
>   PROCINFO["sorted_in"] = "@val_num_desc"
>   asort (arr)
>
> but the sorting order is (contrary to my specifier) ascending.
> So it seems PROCINFO is not considered by the asort() function?
> (Or is that just inappropriately used?)

No it isn't. All I see in the documentation is:

- PROCINFO["sorted_in"] controls the "for" traversal of arrays;
  i.e. for (var in array) and that's it.

- The same values that maybe used with PROCINFO["sorted_in"]
  maybe explicitly passed as an argument to asort/asorti
  to bring about the same sort order.

> Another question; is there a simple way for a _unique sorting_
> like Unix'es 'sort -u'? - By simple I mean some setting, not a
> programmed function like using associative array indexes, etc.

I don't think so, but if we consider that the indices of
an associative array must be unique, then it may be workable
to store the data as indices to "uniquefy" it, and use asorti
to sort indices rather than values.

asorti inverts the array, sort of. The values of the array
are lost. The sorted indices become the values of the target
array, whose indices are the natural numbers (integers from 1).

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Back to comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Sorting with GNU Awk Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-11 03:13 +0100
  Re: Sorting with GNU Awk Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 02:47 +0000
    Re: Sorting with GNU Awk Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-11 05:09 +0100

csiph-web