stable sort

Ron Stanonik stanonik at nprdc.arpa
Fri Dec 23 07:41:07 AEST 1988


One of our users needed a version of sort (we're running
4.3bsd) that is stable; ie, lines considered equal are
grouped together but not otherwise rearranged;  For example,
	0 1
	0 0
sorted on the 0th field would not be rearranged.

As distributed, sort's behavior is (from the man page)
"Lines that otherwise compare equal are ordered with all
bytes significant".

We puzzled through sort and came up with a "fix".  One
question about sort though, any idea why ibuf[256] rather
than ibuf[7], since it never seems to merge more than
7 files at a time?

Thanks,

Ron Stanonik
stanonik at nprdc.arpa

Diffs to make sort stable
*** /usr/src/usr.bin/sort.c	Tue Jun  3 16:59:43 1986
--- sort.c	Tue Dec 20 09:00:45 1988
***************
*** 35,40 ****
--- 35,41 ----
  int 	mflg;
  int	cflg;
  int	uflg;
+ int	Sflg;
  char	*outfil;
  int unsafeout;	/*kludge to assure -m -o works*/
  char	tabchar;
***************
*** 206,211 ****
--- 207,216 ----
  					dirtry[0] = *++argv;
  				continue;
  
+ 			case 'S':
+ 				Sflg++;
+ 				continue;
+ 
  			default:
  				field(++*argv,nfields>0);
  				break;
***************
*** 673,678 ****
--- 678,685 ----
  	}
  	if(uflg)
  		return(0);
+ 	if(Sflg)
+ 		return(i < j ? 1 : -1);
  	return(cmpa(i, j));
  }
  



More information about the Comp.unix.wizards mailing list