    PROGRAM nag_sort_ex08

      ! Example Program Text for nag_sort
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_write_mat, ONLY : nag_write_gen_mat
      USE nag_sort, ONLY : nag_rank_vec, nag_reorder_vec
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC ABS, KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, j, k, m, n
      CHARACTER (40) :: title
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: rank(:)
      COMPLEX (wp), ALLOCATABLE :: a(:,:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_sort_ex08'
      WRITE (nag_std_out,*)

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) m, n, k

      IF (k>=1 .AND. k<=n) THEN

        ALLOCATE (rank(m),a(m,n))  ! Allocate storage

        READ (nag_std_in,*) (a(i,:),i=1,m)

        ! Rank the kth column of matrix in ascending order of modulus

        CALL nag_rank_vec(ABS(a(:,k)),rank)

        ! Rearrange the columns of the matrix
        DO j = 1, n

          CALL nag_reorder_vec(a(:,j),rank)

        END DO

        WRITE (title,'(A,i3)') ' Data sorted on modulus of column', k

        CALL nag_write_gen_mat(a,format='F4.1',int_row_labels=.TRUE., &
         int_col_labels=.TRUE.,title=title)

        DEALLOCATE (a,rank)        ! Deallocate storage

      ELSE

        WRITE (nag_std_in,*) 'k must satisfy  1 <= k <= n'
        WRITE (nag_std_in,'(A,I3,A,I3)') '  where k =', k, '   and n =', n

      END IF

    END PROGRAM nag_sort_ex08
