The generic subroutine implementing the SORT_INDEX algorithm to
return an index array whose elements would sort the input array in the
desired direction. It is primarily intended to be used to sort a
derived type array based on the values of a component of the array.
Its use has the syntax:
array: the rank 1 array to be sorted. It is an intent(inout)
argument of any of the types integer(int8), integer(int16),
integer(int32), integer(int64), real(real32), real(real64),
real(real128), character(*), type(string_type),
type(bitset_64), type(bitset_large). If both the
type of array is real and at least one of the elements is a NaN,
then the ordering of the array and index results is undefined.
Otherwise it is defined to be as specified by reverse.
index: a rank 1 array of sorting indices. It is an intent(out)
argument of the type integer(int_index). Its size shall be the
same as array. On return, if defined, its elements would
sort the input array in the direction specified by reverse.
work (optional): shall be a rank 1 array of the same type as
array, and shall have at least size(array)/2 elements. It is an
intent(out) argument to be used as “scratch” memory
for internal record keeping. If associated with an array in static
storage, its use can significantly reduce the stack memory requirements
for the code. Its value on return is undefined.
iwork (optional): shall be a rank 1 integer array of kind int_index,
and shall have at least size(array)/2 elements. It is an
intent(out) argument to be used as “scratch” memory
for internal record keeping. If associated with an array in static
storage, its use can significantly reduce the stack memory requirements
for the code. Its value on return is undefined.
reverse (optional): shall be a scalar of type default logical. It
is an intent(in) argument. If present with a value of .true. then
index will sort array in order of non-increasing values in stable
order. Otherwise index will sort array in order of non-decreasing
values in stable order.
Examples
Sorting a related rank one array:
subroutine sort_related_data(a,b,work,index,iwork)! Sort `b` in terms or its related array `a`integer,intent(inout)::a(:)integer(int32),intent(inout)::b(:)! The same size as ainteger(int32),intent(out)::work(:)integer(int_index),intent(out)::index(:)integer(int_index),intent(out)::iwork(:)! Find the indices to sort acall sort_index(a,index(1:size(a)),&work(1:size(a)/2),iwork(1:size(a)/2))! Sort b based on the sorting of ab(:)=b(index(1:size(a)))end subroutine sort_related_data
Sorting a rank 2 array based on the data in a column
subroutine sort_related_data(array,column,work,index,iwork)! Sort `a_data` in terms or its component `a`integer,intent(inout)::a(:,:)integer(int32),intent(in)::columninteger(int32),intent(out)::work(:)integer(int_index),intent(out)::index(:)integer(int_index),intent(out)::iwork(:)integer,allocatable::dummy(:)integer::iallocate(dummy(size(a,dim=1)))! Extract a component of `a_data`dummy(:)=a(:,column)! Find the indices to sort the columncall sort_index(dummy,index(1:size(dummy)),&work(1:size(dummy)/2),iwork(1:size(dummy)/2))! Sort a based on the sorting of its columndo i=1,size(a,dim=2)a(:,i)=a(index(1:size(a,dim=1)),i)end do end subroutine sort_related_data
Sorting an array of a derived type based on the dsta in one component
subroutine sort_a_data(a_data,a,work,index,iwork)! Sort `a_data` in terms or its component `a`type(a_type),intent(inout)::a_data(:)integer(int32),intent(inout)::a(:)integer(int32),intent(out)::work(:)integer(int_index),intent(out)::index(:)integer(int_index),intent(out)::iwork(:)! Extract a component of `a_data`a(1:size(a_data))=a_data(:)%a! Find the indices to sort the componentcall sort_index(a(1:size(a_data)),index(1:size(a_data)),&work(1:size(a_data)/2),iwork(1:size(a_data)/2))! Sort a_data based on the sorting of that componenta_data(:)=a_data(index(1:size(a_data)))end subroutine sort_a_data
Nodes of different colours represent the following:
Solid arrows point from a submodule to the (sub)module which it is
descended from. Dashed arrows point from a module or program unit to
modules which it uses.
Where possible, edges connecting nodes are
given different colours to make them easier to distinguish in
large graphs.