df_sort_by_column Subroutine

public subroutine df_sort_by_column(df, col_index, ascending)

Sort data frame by column values

Sorts all rows of the data frame based on values in the specified column. Only real and integer columns can be used for sorting.

@param[in,out] df The data frame instance @param[in] col_index Index of column to sort by @param[in] ascending Optional sort direction (default: .true. for ascending)

Note

This operation modifies the data frame in place

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(inout) :: df
integer, intent(in) :: col_index
logical, intent(in), optional :: ascending

Source Code

    subroutine df_sort_by_column(df, col_index, ascending)
        type(data_frame), intent(inout) :: df
        integer, intent(in) :: col_index
        logical, intent(in), optional :: ascending

        logical :: asc
        integer, allocatable :: indices(:)
        integer :: i
        type(column) :: col

        asc = .true.
        if (present(ascending)) asc = ascending

        if (col_index < 1 .or. col_index > df % ncols()) then
            error stop "Column index out of range in df_sort_by_column"
        end if

        allocate (indices(df % nrows()))
        do i = 1, df % nrows()
            indices(i) = i
        end do

        col = df % get_data_col(col_index)

        select case (col % get_type())
        case (REAL_NUM)
            call sort_indices_real(col % getr(), indices, asc)
        case (INTEGER_NUM)
            call sort_indices_integer(col % geti(), indices, asc)
        case default
            error stop "Sorting only supported for real and integer columns"
        end select

        call reorder_all_columns(df, indices)

        deallocate (indices)
    end subroutine df_sort_by_column