C******************************************************************************
	SUBROUTINE AK$SORT
C******************************************************************************
C AKSORT.FOR
C
C  written by:  K. Trumbley
C  date:        Oct 1984
C  modified:	K. Trumbley, fix TOT_USR so all records sorted
C
	INCLUDE 'AK.INC/NOLIST'
	INCLUDE 'BK.INC/NOLIST'
	LOGICAL*1 FLIP,FLOP
C
C
C  Sort array of users depending on SORT_TYPE
C
	IF (TOT_USR.LT.2) RETURN
C
	FLOP=.TRUE.
	DO WHILE(FLOP)
	  FLOP=.FALSE.
	  FLIP=.FALSE.
C
	  DO I=1,TOT_USR-1
	    AK$RECORD=AK$SUMMARY(I)
	    BK$RECORD=AK$SUMMARY(I+1)
	    IF (ASCEND_FLAG) THEN
	      IF (SORT_TYPE.EQ.'USERNAME') THEN
	        IF (AK$USR_NAM.GT.BK$USR_NAM) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'ACCOUNT') THEN
	        IF (AK$USR_ACC.GT.BK$USR_ACC) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'UIC') THEN
	        IF ((AK$USR_GRP.GT.BK$USR_GRP).OR.
     &	            (AK$USR_GRP.EQ.BK$USR_GRP.AND.
     &	             AK$USR_MEM.GT.BK$USR_MEM)) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'DISK_USAGE') THEN
	        IF (AK$DSK_BLK.GT.BK$DSK_BLK) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'PRINTED_PAGES') THEN
	        IF (AK$PRT_PAG.GT.BK$PRT_PAG) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'CHARGES') THEN
	        IF (AK$CHARGE.GT.BK$CHARGE) FLIP=.TRUE.
	      END IF
	    ELSE
	      IF (SORT_TYPE.EQ.'USERNAME') THEN
	        IF (AK$USR_NAM.LT.BK$USR_NAM) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'ACCOUNT') THEN
	        IF (AK$USR_ACC.LT.BK$USR_ACC) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'UIC') THEN
	        IF ((AK$USR_GRP.LT.BK$USR_GRP).OR.
     &	            (AK$USR_GRP.EQ.BK$USR_GRP.AND.
     &	             AK$USR_MEM.LT.BK$USR_MEM)) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'DISK_USAGE') THEN
	        IF (AK$DSK_BLK.LT.BK$DSK_BLK) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'PRINTED_PAGES') THEN
	        IF (AK$PRT_PAG.LT.BK$PRT_PAG) FLIP=.TRUE.
	      ELSE IF (SORT_TYPE.EQ.'CHARGES') THEN
	        IF (AK$CHARGE.LT.BK$CHARGE) FLIP=.TRUE.
	      END IF
	    END IF
	    IF (FLIP) THEN
	      AK$SUMMARY(I+1)=AK$RECORD
	      AK$SUMMARY(I)=BK$RECORD
	      FLIP=.FALSE.
	      FLOP=.TRUE.
	    END IF
	  END DO
	END DO
	RETURN
	END
