Sorting by Functions in Co-array Fortran

program sorting_by_functions
  use iso_fortran_env, only: int32, real32
  implicit none

  type :: string_type
    character(len=:), allocatable :: str
  end type string_type

  type :: person_type
    character(len=:), allocatable :: name
    integer :: age
  end type person_type

  type(string_type), allocatable :: fruits(:)
  type(person_type), allocatable :: people(:)
  integer :: i

  ! Initialize fruits array
  allocate(fruits(3))
  fruits(1)%str = "peach"
  fruits(2)%str = "banana"
  fruits(3)%str = "kiwi"

  ! Sort fruits by length
  call sort_by_length(fruits)
  
  print *, "Sorted fruits:"
  do i = 1, size(fruits)
    print *, trim(fruits(i)%str)
  end do

  ! Initialize people array
  allocate(people(3))
  people(1) = person_type("Jax", 37)
  people(2) = person_type("TJ", 25)
  people(3) = person_type("Alex", 72)

  ! Sort people by age
  call sort_by_age(people)

  print *, "Sorted people:"
  do i = 1, size(people)
    print *, trim(people(i)%name), people(i)%age
  end do

contains

  subroutine sort_by_length(arr)
    type(string_type), intent(inout) :: arr(:)
    integer :: i, j
    type(string_type) :: temp

    do i = 1, size(arr) - 1
      do j = i + 1, size(arr)
        if (len_trim(arr(i)%str) > len_trim(arr(j)%str)) then
          temp = arr(i)
          arr(i) = arr(j)
          arr(j) = temp
        end if
      end do
    end do
  end subroutine sort_by_length

  subroutine sort_by_age(arr)
    type(person_type), intent(inout) :: arr(:)
    integer :: i, j
    type(person_type) :: temp

    do i = 1, size(arr) - 1
      do j = i + 1, size(arr)
        if (arr(i)%age > arr(j)%age) then
          temp = arr(i)
          arr(i) = arr(j)
          arr(j) = temp
        end if
      end do
    end do
  end subroutine sort_by_age

end program sorting_by_functions

This program demonstrates custom sorting in Co-array Fortran. Here’s an explanation of the key parts:

  1. We define custom types string_type and person_type to represent strings and people, respectively.

  2. We create arrays of these custom types: fruits for strings and people for persons.

  3. The sort_by_length subroutine implements a simple bubble sort algorithm to sort the fruits array based on the length of each string.

  4. The sort_by_age subroutine similarly sorts the people array based on the age of each person.

  5. In the main program, we initialize the arrays, call the sorting subroutines, and then print the sorted results.

To run this program:

  1. Save the code in a file named sorting_by_functions.f90.

  2. Compile the program using a Fortran compiler that supports Co-array Fortran. For example, with gfortran:

    $ gfortran -fcoarray=single sorting_by_functions.f90 -o sorting_by_functions
  3. Run the compiled program:

    $ ./sorting_by_functions

The output should display the fruits sorted by length and the people sorted by age.

Note that Co-array Fortran is primarily used for parallel programming, but this example doesn’t utilize its parallel features. The sorting algorithms used here are simple and not optimized. In a real-world scenario, you might want to use more efficient sorting algorithms or leverage Co-array Fortran’s parallel capabilities for large datasets.