10.3.4 sortArray (sorts array data)
Syntax
sortArray [-d element-number] [-n] [-r] array-name-1 array-name-2
Description
This command sorts the value of the array specified for argument. Characters contained in the value are case-sensitive.
Arguments
- -d element-number
-
This command sorts the value of two-dimensional array corresponding to the specified element number. To sort values of all two-dimensional array elements, specify "@" for the element number. If you specify "@", the values of elements in each array corresponding to the first element number of two-dimensional array are sorted. For example, if the two-dimensional array is 2 x 3, sort a value of elements among array[0][0], array[0][1] and array[0][2] and sort a value of elements among array[1][0], array[1][1] and array[1][2].
If you do not specify this option, interpret the array as one-dimensional array and sort the array.
- -n
-
This command interprets the leading value character string as numerical values and sorts values.
- -r
-
This command specifies sorting in descending order.
If you specify this option, the default is sorting in ascending order.
- array-name-1
-
This command specifies the array names to be sorted.
- array-name-2
-
This command specifies the name of array in which sorted data is stored.
Output to the standard output
None
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
Not less than 1 |
Termination with an error |
Notes
-
If values in the array to be sorted contain line breaks, arrays are not sorted correctly.
-
If the arrays to be sorted have elements that do not have any value, the element is deleted and arrays are sorted.
-
Although you can specify "@" or a value ranging from 0 to 65535 for the element number of argument, but whether the character string can be specified is not verified.
-
Do not specify an array name starting with "adsh" for the array-name 1 or array-name 2 argument. If you specify such an array name, it might conflict with the name of another variable used within the part and produce invalid output.
-
The character string that can be specified for array-name-1 and array-name-2 are the same as character string that can be used as variable name. However, whether the character string can be specified is not checked.
Examples
set -A input Tokyo Yokohama Fukuoka Nagoya sortArray input output echo "${output[@]}" # "Fukuoka Nagoya Tokyo Yokohama" is output. set -A input Tokyo Yokohama Fukuoka Nagoya sortArray -r input output echo "${output[@]}" # "Yokohama Tokyo Nagoya Fukuoka" is output. set -A input -- -3 70 -50 8 100 sortArray -n input output echo "${output[@]}" # "-50 -3 8 70 100" is output. set -D input { Osaka Fukuoka Nagoya } { Tokyo Yokohama Chiba } sortArray -d 1 input output echo "${output[0][@]}" # "Osaka Fukuoka Nagoya" is output. echo "${output[1][@]}" # "Chiba Tokyo Yokohama" is output. set -D input { Osaka Fukuoka Nagoya } { Tokyo Yokohama Chiba } sortArray -d @ input output echo "${output[0][@]}" # "Fukuoka Nagoya Osaka" is output. echo "${output[1][@]}" # "Chiba Tokyo Yokohama" is output.