function sort_test01 ( )

%*****************************************************************************80
%
%% SORT_TEST01 shows how to sort a matrix related to a vector.
%
%  Discussion:
%
%    Given a vector of eigenvalues V and the matrix M whose columns are the
%    corresponding eigenvectors, we may wish to sort V, then sort M in
%    accordance with the sorting of V.
%
%    MATLAB's sort() function can return the necessary sorting index.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 December 2018
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'SORT_TEST01\n' );
  fprintf ( 1, '  Sort a vector V, and correspondingly sort a matrix M.\n' );

  m = 5;
  n = 5;

  v = [ ...
    0.6986; ...
    0.2340; ...
   10.9753; ...
    0.8894; ...
    0.0004 ];

  r8vec_print ( n, v, '  Vector V:' );

  a = [ ...
   -0.0530,   -0.0884,    0.4307,    0.8839,    0.1503; ...
   -0.1961,   -0.7322,    0.4309,   -0.2207,   -0.4370; ...
    0.7467,    0.3098,    0.4617,   -0.0890,   -0.3539; ...
    0.0798,   -0.1023,    0.4730,   -0.3701,    0.7890; ...
   -0.6283,    0.5913,    0.4380,   -0.1585,   -0.1968 ];

  r8mat_print ( m, n, a, '  Matrix A:' );

  av = a * v;

  r8vec_print ( m, av, '  Product A*V:' );
%
%  Sort the vector, and save the sorting index.
%
  [ vs, vi ] = sort ( v );
  i4vec_print ( n, vi, '  Sorting index VI:' );

  r8vec_print ( n, vs, '  Sorted vector VS:' );
%
%  Use the sorting index on the columns of a.
%
  as = a(:,vi);
  r8mat_print ( m, n, as, '  Sorted matrix AS:' );

  asvs = as * vs;

  r8vec_print ( m, asvs, '  Product AS*VS:' );

  return
end

