An interesting sort problem

jd1008 jd1008 at gmail.com
Fri Nov 27 22:28:48 UTC 2015



On 11/27/2015 01:28 PM, Jon LaBadie wrote:
> On Fri, Nov 27, 2015 at 12:38:38PM -0700, jd1008 wrote:
>>
>> On 11/27/2015 12:14 PM, Jon LaBadie wrote:
>>> On Fri, Nov 27, 2015 at 09:57:11AM -0700, jd1008 wrote:
>>>> On 11/27/2015 03:29 AM, Andrew Haley wrote:
>>>>> #!/bin/awk -f
>>>>> {
>>>>>       lines[NR]=$NF " " $0
>>>>> }
>>>>>
>>>>> END {
>>>>>       PROCINFO["sorted_in"]="@val_type_asc"
>>>>> #      for (i in lines) {
>>>           for (i = NR; i >= 1; i--) {
>>>>>           line = lines[i]
>>>>>           j=index(line, " ")
>>>>>           print substr(line, j+1)
>>>>>       }
>>>>> }
>>>> Hi Andrew,
>>>> manpage for awk does not betray any info on making sort in reverse.
>>>> What is the incantation in this script to make the sort in reverse?
>> Sorry John. It did not sort in reverse :(
>>
> Whoops, sorry, I forgot PROCINFO doesn't sort the array,
> it affects how the array is accessed.
>
> Does this work?  Choose which ever function suits your direction.
>
>
> #!/bin/awk -f
> {
>        lines[NR]=$NF " " $0
> }
>
> function cmp_str_index(i1, v1, i2, v2)
> {
>      # string index comparison, ascending order
>      v1 = v1 ""
>      v2 = v2 ""
>      if (v1 < v2)
> 	return -1
>      return (v1 != v2)
> }
>
> function rcmp_str_index(i1, v1, i2, v2)
> {
>      # string index comparison, decending order
>      v1 = v1 ""
>      v2 = v2 ""
>      if (v2 < v1)
> 	return -1
>      return (v2 != v1)
> }
>
>
> END {
>       PROCINFO["sorted_in"] = "rcmp_str_index"
>       for (i in lines) {
>            line = lines[i]
>            j=index(line, " ")
>            print substr(line, j+1)
>       }
> }
>
> jl
YEP!!!
That does it!!!
Thanx a lot!



More information about the users mailing list