    PROGRAM nag_sort_ex02

      ! 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 KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, j, k, m, n
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: rank(:)
      REAL (wp), ALLOCATABLE :: a(:,:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_sort_ex02'
      WRITE (nag_std_out,*)

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

      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

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

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

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

      END DO

      CALL nag_write_gen_mat(a,format='F6.1',title='Data sorted on column 1')

      DEALLOCATE (a,rank)          ! Deallocate storage

    END PROGRAM nag_sort_ex02
