Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • I assume clunky workarounds like printing to a temp file, then using sort and reading the file are not what you're after, right? Commented Nov 19, 2023 at 16:36
  • 1
    Note that your "pop" and "dequeue" operations are the same. There's no reasonable way remove an element off the end of the list. (In Bash, you could do set -- "${@:1: $#-1}", but it's not POSIX and will croak if the list if empty to begin with.) This is probably one of those things that would be better done in another programming language with better support for data structures. Commented Nov 19, 2023 at 17:15
  • That is to say, POSIX sh only allows indexing into the list of positional parameters for getting the values, but you can't assign values in the middle, and you can't remove them from the middle. Any algorithm that involves swapping list items is pretty much impossible to use. You could swap the first two with something like x=$1 y=$2; shift; shift; set -- "$y" "$x" "$@", though, but generalizing that to an arbitrary pair, or even an arbitrary consecutive pair gets rather difficult. Commented Nov 19, 2023 at 17:23
  • You could move the values to variables called e1, e2 etc. and do eval tricks to "index" that, but that way lies madness. You can't really even shove the values to an outside program (like sort), since to be able to hold arbitrary values, you'd need to read NUL-separated output back from the program, and that's not really easy POSIXly. Commented Nov 19, 2023 at 17:25