What is the sort() method in the Euphoria language?

Overview

The sort() method in the Euphoria programming language is used to arrange the members of a sequence in the desired order.

In this method, sorting can be ascending or descending, indicated by the inbuilt constants ASCENDING and DESCENDING.

Syntax

sort(to_be_sorted, sort_order)

Parameters

The sort() method takes the following parameters:

  • to_be_sorted: The sequence to be sorted.
  • sort_order: Indicates the way we want to sort the given sequence. It is indicated by ascendingMembers of the sequence are arranged from the smallest member to the largest in value. or descendingMembers of the sequence are arranged from the largest member to the smallest in value.. They are inbuilt constants of Euphoria’s sort.e standard library file. The default value of this parameter is ASCENDING.

To use the sort() method, we must include the sort.e file from the standard library of Euphoria like this:

include std/sort.e

Return value

This method returns a sorted sequence.

Code

Let’s look at the code below:

include std/sort.e
--define variables
constant model_ages = {18,32,16,23,17,16,25,20,19,32,15}
sequence sorted_model_agesASC,sorted_model_agesDESC
--store the outcome of sort operation
sorted_model_agesASC = sort( model_ages )
sorted_model_agesDESC = sort( model_ages,DESCENDING)
--print outcome to screen
printf(1,"This is a sort in ASCENDING order: ")
print(1,sorted_model_agesASC)
puts(1,"\n")
printf(1,"This is a sort in DESCENDING order: ")
print(1,sorted_model_agesDESC)

Explanation

In the code above,

  • Line 1: We include the sort.e file.
  • Lines 4 and 5: We define the variables.
  • Lines 8 and 9: We sort the data in indicated variables and save the outcome.
  • Lines 12 to 16: We display necessary details on the screen.