Hello. In ksh sort, how can u specify a stable sort?
Thanks.
DS
> Hello. In ksh sort, how can u specify a stable sort?
(1) What is a "stable sort"?
(2) Why do you not think the man page answers your question?
A crude way of simulating a stable sort is:
nl -ba file | sort -k2 | cut -d' ' -f2-
where the character between the 's is a TAB, and the
"-k2" option should be whatever sort criteria you need,
with adjustment for the added leading line number.
N.B.: since the question was asked by another --- a sort
is said to be "stable" if two records whose keys are equal
maintain the same relative ordering after the sort as they
possessed before the sort. Like the above simulation shows,
this is equivalent to augmenting the key with the original
record number (as the lowest-priority comparison).
--Ken Pizzini
>> Hello. In ksh sort, how can u specify a stable sort?
Nosey, eh ...Quote:>I am curious:
key1 value1Quote:>(1) What is a "stable sort"?
... a stable sort gurantees the first three lines stay in that order
if sorted by key# (all key1's assumed to be the same). Bubble sort
is stable IIRC, qsort() is not.
Cannot answer this one. Used man sort to look the -s option up too,Quote:>(2) Why do you not think the man page answers your question?
Cheers,
Juergen
--
\ Real name : Jrgen Heinzl \ no flames /
[ snip ]
> ... a stable sort gurantees the first three lines stay in that order
> if sorted by key# (all key1's assumed to be the same). Bubble sort
> is stable IIRC, qsort() is not.
> >(2) Why do you not think the man page answers your question?
> Cannot answer this one. Used man sort to look the -s option up too,
> tricks of the trade, but don't tell 8)
>[ snip ]
>> ... a stable sort gurantees the first three lines stay in that order
>> if sorted by key# (all key1's assumed to be the same). Bubble sort
>> is stable IIRC, qsort() is not.
>> >(2) Why do you not think the man page answers your question?
>> Cannot answer this one. Used man sort to look the -s option up too,
>> tricks of the trade, but don't tell 8)
>Hmm... My sort (Solaris) (1) doesn't have a -s option and (2) explicitly
>states that stability is not guaranteed -- so maybe the original
>poster's question isn't so dumb after all. Although, as I recall, one
>can force stability by using 'cat -n' to number the input lines,
>changing the key definition to include the line number as the last field
>in the key, and cut to remove the line number from the output.
Cheers,
Juergen
--
\ Real name : Jrgen Heinzl \ no flames /
Hello. In ksh sort, how can u specify a stable sort?
Thanks.
DS
P.S. Remove REMOVE in email address.
5. sort sort: 0653-657 A write error occurred while sorting (4.1.3)
8. Patch to (x86) 2.4.21 asm/system.h to suppress warning from GCC 3.3.
9. sort: write error while sorting: No space left on device??
10. "sort" sorting uppercase/lowercase differently on different boxes
12. default secondary sort line in 'sort'
13. (sort | uniq) vs (sort -u)