indermau@dg (Kurt Indermaur) (05/18/88)
I'm stuck with a little sorting problem... not big enough to write my own sorting routine, but annoying nevertheless. I have a file with lines like: 1237 A2 text comments 1357 A3 more text 1644 A1 blah blah blah 1718 A1 still more (sorted by the number in the first column) that I would like to get sorted by the classification in the second column (A1, A2, A3...), but keeping the numbers in order within each classification. The above list, for instance, would end up: 1644 A1 blah blah blah 1718 A1 still more 1237 A2 text comments 1357 A3 more text I need a stable sort, in other words, and the standard (4.3 bsd) UNIX sort doesn't seem to provide that. Is there a way around this using UNIX tools? Is there another way? Any help (including programs, if necessary) would be appreciated. Please respond by e-mail... I don't often get the chance to read this group. Thanks, Kurt Indermaur (indermau@dg.cs.umn.edu)
ries@trwrb.UUCP (Marc Ries) (05/18/88)
>I'm stuck with a little sorting problem... not big enough to write my own [...] >(sorted by the number in the first column) that I would like to get >sorted by the classification in the second column (A1, A2, A3...), but >keeping the numbers in order within each classification. The above list, >for instance, would end up: > >1644 A1 blah blah blah >1718 A1 still more >1237 A2 text comments >1357 A3 more text > >I need a stable sort, in other words, and the standard (4.3 bsd) UNIX sort >doesn't seem to provide that. Is there a way around this using UNIX tools? >Is there another way? Any help (including programs, if necessary) would be >appreciated. > In the past, "sort +1 -2 +0 -1 data_file" seemed to work ok 8-). -- Marc A. ries@trwrb.TRW.COM sdcrdcf!---\ ihnp4!------\----- trwrb! --- ries
morrell@hpsal2.HP.COM (Michael Morrell) (05/20/88)
/ hpsal2:comp.unix.questions / ries@trwrb.UUCP (Marc Ries) / 9:30 am May 18, 1988 / >I'm stuck with a little sorting problem... not big enough to write my own [...] >(sorted by the number in the first column) that I would like to get >sorted by the classification in the second column (A1, A2, A3...), but >keeping the numbers in order within each classification. The above list, >for instance, would end up: > >1644 A1 blah blah blah >1718 A1 still more >1237 A2 text comments >1357 A3 more text > >I need a stable sort, in other words, and the standard (4.3 bsd) UNIX sort >doesn't seem to provide that. Is there a way around this using UNIX tools? In the past, "sort +1 -2 +0 -1 data_file" seemed to work ok 8-). --- Sort does not have a way to perform a stable sort. I have felt for a long time that there should be an option to do this (-s?). With your example, doing what Marc Ries suggests will work because it re-sorts the 1st colummn (which was already sorted in the input). The problem occurs if the input order cannot be recreated by sorting. For now, the following kludge is the only way I know of to get a stable sort: awk '{print NR "\t" $0}' data_file | sort +2 -3 +0n -1 | cut -f2- This prefixes each line by its line number, sorts using the line number as the final sort key, then strips off the line number. Michael Morrell