    PROGRAM nag_sort_ex01

      ! 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_sort, ONLY : nag_sort_vec, nag_rank_vec, nag_reorder_vec
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Local Scalars ..
      INTEGER :: n
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: rank(:)
      CHARACTER (12), ALLOCATABLE :: a(:), a1(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_sort_ex01'
      WRITE (nag_std_out,*)

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

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

      READ (nag_std_in,'(A)') a
      a1 = a

      ! Sort data directly

      CALL nag_sort_vec(a,pos1=7,pos2=11,ascend=.FALSE.)

      WRITE (nag_std_out,*) 'Data sorted (directly) on columns 7 to 11'
      WRITE (nag_std_out,'(2X,A)') a
      WRITE (nag_std_out,*)

      ! Sort data indirectly (by ranking and reordering)

      CALL nag_rank_vec(a1(:)(7:11),rank,ascend=.FALSE.)

      CALL nag_reorder_vec(a1,rank)

      WRITE (nag_std_out,*) 'Vector of ranks'
      WRITE (nag_std_out,'(2X,15I4)') rank
      WRITE (nag_std_out,*)

      WRITE (nag_std_out,*) 'Data sorted (indirectly) on columns 7 to 11'
      WRITE (nag_std_out,'(2X,A)') a1

      DEALLOCATE (a,a1,rank)       ! Deallocate storage

    END PROGRAM nag_sort_ex01
