I'm trying to figure out how to make a function that can take an array as a parameter and sort it. I think it is done with positional variables, but I'm not sure.
|
Sort the easy way with
Into a new array:
Without help by
For 20 numbers, bubblesort might be sufficient. |
|||||||||||||||||||||
|
bashI don't think If we consider that array elements can contain any byte value but 0 in bash-4.4 (still in beta) makes it easier as it introduced a To sort array
would sort the array reliably. Use the To implement your
With earlier versions of
For the
zshZsh has builtin support to sort arrays. you can use the
In locales that don't already sort case-independently, you can also add the To assign to an array:
So a
AT&T ksh (ksh88 or ksh93, both of which can be found as sh on some systems)
A
|
||||
|
Here's a slightly more complicated, and somewhat slower version which nevertheless does not squeeze duplicates and which sorts (reasonably sized) decimal numbers in numeric order - though (space-split) other strings are still sorted, string length is considered first. And to handle generic strings you'd almost definitely want to set the I'll be honest - I would (maybe) consider sorting a list of words or numbers like this, but it wouldn't otherwise occur to me to create a file with a name that wouldn't at least fit comfortably within a paragraph. And so it splits on spaces. Most often, that's the right thing to do. It is, however, also hampered by a sanity requirement of treating
If there's any lesson in this, maybe it should be what a filthy thing bash arrays are in the first place. If data was simply kept in files we'd never have any issue |
|||||
|
Two weird, in-memory plain-bash solutions. Benchmark for many answers given in this question is available on gists, with results available in the comment area. I may update those things with copypastes from new answers irregularly. All the complexity calculations ignore the length of strings in bash. For
|
|
kind of all of the stuff is. i thought about that too -
export -p and set and alias . but it involved a lot of parsing i didnt want to do, and so i just made a blank directory. i dont use bash arrays though and so it didnt occur to me - its a pretty good answer - a lot less parsing than i would have wound up doing with that other stuff.
– mikeserv
Dec 8 at 16:37
|
||
|
@StéphaneChazelas It appears it's my fault.. My memory got wrong. Let me try some indexed array though -- perhaps with number-indexed ones I can still get sorted output.
– Arthur2e5
Dec 8 at 17:21
|
||
|
|||
|
yeah! sortnums is slow. yours is pretty good. i figured out how to do it with
alias like eval "unalias -a; alias $(printf "%d= " "$@" 2>/dev/null); alias" | cut -d= -f1 - i think that should work (but in bash you might need set -o posix first) - it will eat dups, though, of course.
– mikeserv
Dec 9 at 5:09
|
||
|
oh! i didnt notice the Cygwin thing - i dont think it works as a good control system for benching anything but native shell apps. cygwin does this weird file-system abstraction layer, and doesn't have a true analog for a unix fork. i'll try it too.
– mikeserv
Dec 9 at 6:05
|