You Asked: How Do I Sort an Array in Linux?
Got questions about You Asked: How Do I Sort an Array in Linux? Read the latest insights for easy understanding.
Contents
How do you sort an array in Unix?
“${array[*]}” <<< sort. sorted=($(…))
…
- Open an inline function {…} to get a fresh set of positional arguments (e.g. $1 , $2 , etc).
- Copy the array to the positional arguments. …
- Print each positional argument (e.g. printf ‘%sn’ “[email protected]” will print each positional argument on its own line. …
- Then sort does its thing.
Must Read
How do you sort an array array?
If you wanted to sort on both elements of each sub-array (ie. sort by the first element descending, then if they are the same then sort by the second element descending), you could do this: var sortedArray = array. sort(function(a, b) { if (a[0] == b[0]) { return a[1] – b[1]; } return b[0] – a[0]; });