Sort associative array with AWK

后端 未结 6 1434
逝去的感伤
逝去的感伤 2020-12-29 08:31

Here\'s my array (gawk script) :

myArray[\"peter\"] = 32
myArray[\"bob\"] = 5
myArray[\"john\"] = 463
myArray[\"jack\"] = 11

After sort, I

6条回答
  •  一生所求
    2020-12-29 08:34

    The authors of The Awk Programming Language provide a quicksort function, which is available online.

    I think you'd do something like this.

    END {
      for (key in myArray) {
        sortkeys[j++] = key;
      }
      qsort(sortkeys, 0, length(myArray));      # Not sure I got the args right.
      for (i = 1; i <= length(myArray); i++) {
        print sortkeys[i], myArray[sortkeys[i]];
      }
    }
    

提交回复
热议问题