Sort

array_sort

Sort a named array in place using lexicographic order.

Example

myarr=( banana apple cherry )
array_sort myarr
printf '%s\n' "${myarr[@]}"
# => apple
# => banana
# => cherry

Arguments

  • $1 (string): Name of the array variable.

Exit codes

  • 0: Always

array_sort_numeric

Sort a named array in place using numeric order.

Arguments

  • $1 (string): Name of the array variable.

Exit codes

  • 0: Always

array_sort_natural

Sort a named array in place using natural (version) order. Numbers embedded in strings are compared numerically, so file2 sorts before file10 and v1.9 sorts before v1.10. Requires GNU coreutils sort -V. If unavailable, falls back to standard lexicographic sort and prints a warning to stderr.

Example

files=( file10.txt file2.txt file1.txt )
array_sort_natural files
printf '%s\n' "${files[@]}"
# => file1.txt
# => file2.txt
# => file10.txt

Arguments

  • $1 (string): Name of the array variable.

Exit codes

  • 0: Always

array_reverse

Reverse the order of elements in a named array in place.

Example

myarr=( a b c d e )
array_reverse myarr
printf '%s\n' "${myarr[@]}"
# => e
# => d
# => c
# => b
# => a

Arguments

  • $1 (string): Name of the array variable.

Exit codes

  • 0: Always

array_shuffle

Shuffle a named array in place using the Fisher-Yates algorithm.

Arguments

  • $1 (string): Name of the array variable.

Exit codes

  • 0: Always