Swift Program to Sort Elements in Lexicographical Order (Dictionary Order)

This tutorial will discuss how to write a Swift program to sort elements in lexicographical order(Dictionary order).

An arrangement of the words, characters or numbers in alphabetical order staring from A to Z is known as lexicographic order. It is also known as dictionary order because the searching of the words are same as we search in the real dictionary. In lexicographical order, the words whose first letter is same are arranged in the same group and in the group words are sorted according to their second letter and so on.

To sort the given list of elements into lexicographic order Swift provide a built-in function named sort(). This function is used to sort the elements of the array in lexicographic order.

You can sort the elements either in ascending or deceasing order. By default this function sorts the elements in ascending order.

Syntax

Following is the syntax of the function ?

arrayVar.sort()

Below is a demonstration of the same ?

Input

Suppose our given input is ?

List of words are - ["Apple", "Apricot", "Avocado", "Amla"]

Output

The desired output would be ?

Amla, Apple, Apricot, Avocado

Algorithm

Following is the algorithm ?

  • Step 1 ? Declare an array variable with values.

  • Step 2 ? Print the original array using for loop.

  • Step 3 ? Sort the elements of the array in lexicographical order(Dictionary order) using sort() function.

myWords.sort()
  • Step 4 ? Print the output.

Example

The following program shows how to sort elements in lexicographical order(Dictionary order).

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token keyword">var</span> myWords <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Apple"</span><span class="token punctuation">,</span> <span class="token string">"Apricot"</span><span class="token punctuation">,</span> <span class="token string">"Kiwi"</span><span class="token punctuation">,</span> <span class="token string">"Banana"</span><span class="token punctuation">,</span> <span class="token string">"Mango"</span><span class="token punctuation">,</span> <span class="token string">"Avocado"</span><span class="token punctuation">,</span>
<span class="token string">"Beetroot"</span><span class="token punctuation">,</span> <span class="token string">"Amla"</span><span class="token punctuation">]</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Original List:"</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> myWords<span class="token punctuation">{</span>
   <span class="token function">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// Sorting elements in lexicographical order</span>
myWords<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\nSorted List(lexicographical order):"</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> j <span class="token keyword">in</span> myWords<span class="token punctuation">{</span>
   <span class="token function">print</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Original List:                                
Apple 
Apricot 
Kiwi 
Banana 
Mango 
Avocado
Beetroot
Amla

Sorted List(lexicographical order):
Amla
Apple
Apricot
Avocado
Banana
Beetroot
Kiwi
Mango

Here, in the above code, we have an array named myWords with value now we change the order of the array in lexicographic order using sort() function and display the output.

Updated on: 2022-08-18T13:07:44+05:30

741 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements