	subroutine mysub(a,lda,m,n,js,na,w)
	real*8 a(lda,n),w(m)
	integer na(m)

c.. sorts matrix a, keeping rows together,
c   so that column j is in increasing order

	do i = 1,m
	   na(i) = i
	end do
	call sort(na,1,m,a(1,js),m)
	do j = 1,n
	   do i = 1,m
		w(i) = a(na(i),j)
	   end do
	   do i = 1,m
		a(i,j) = w(i)
	   end do
	end do
	return
	end

c-------------------------------------

      subroutine sort(na,llo,lhi,q,nq)
	real*8 q,ql
      dimension na(lhi),q(nq)
c
c    sorts array na(l), l = llo to lhi
c    according to values of q(na(l))
c    so if llo = 1 and na(l) = l on entry,
c    obtain na(l) = index of l-th smallest q on exit.
c
      if(llo.ge.lhi) return
      mesh = lhi-llo+1
    1 mesh = (mesh+1)/3
      lst = llo+mesh
      do 3 l1 = lst,lhi
      nal = na(l1)
      ql = q(nal)
      lnow = l1
      do 2 l2 = lst,l1,mesh
      lnext = lnow-mesh
      nan = na(lnext)
      if(ql.ge.q(nan)) go to 3
      na(lnow) = nan
    2 lnow = lnext
    3 na(lnow) = nal
      if(mesh.gt.1) go to 1
      return
      end