O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sort array by values using asort( )

GAWK provides a built-in function, asort( ), to sort the array on values. Numeric values come before string values in the sorting order. The following is the syntax for sorting the array:

Gn = asort(arr)

In the following example, we will use the asort() function to sort the array elements, as per the values assigned to them. The only side effect of using the asort() function with default parameters is that the array's original indexes will be lost and the new array is assigned an index from 1 to n as follows:

$ vi asort_arr.awkBEGIN    {    arr[30] = "volvo"    arr[10] = "bmw"    arr[20] = "audi"    arr[50] = "toyota"    arr["car"] = "ferrari"    arr[70]    = "renault"    arr[110] = 20    arr[40]    = "ford"    arr["num"] = 10 arr[80] = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required