Swift Program to Sort a String


In Swift, sorting of a string means arranging the characters of the string in the specified order either in ascending or descending order. For example −

Input: String = "color of the bird is pink"
Output:"      bcdefhiiiklnoooprrst"

Here, the resultant string is sorted in ascending order.

So to sort a string Swift provides an inbuilt function named sorted() function. This function returns a sorted string in which the characters of the string are stored in either ascending or descending order. This function can be defined in two types with and without parameters.

Note that the sorted() function does not remove whitespace characters, it puts all the whitespace characters at the start of the resultant string.

Syntax 1

func sorted()
or 
func sorted(by:)

This function does not take any parameter and returns a sorted string in ascending order.

Syntax 2

func sorted(by:)

Here, this function takes only one parameter either > or < operator. If we pass the < operator in the “by” parameter, then this method sorts the string in ascending order. If we pass the > operator in the “by” parameter, then this method sorts the string in descending order.

Algorithm

  • Step 1 − Create a function that takes an input string and returns a stored string.

  • Step 2 − Inside the function, we use the sorted() function to sort the string.

  • Step 3 − Then return the result by converting it into the string using the String() initializer.

  • Step 4 − Create an input string.

  • Step 5 − Call the function and pass the string into it.

  • Step 6 − Display the output.

Example 1

In the following Swift program, we will sort a string. So create a function which takes the input string as a parameter and returns a sorted string. Inside the function, we will use the sorted() function to sort all the characters of the string in ascending order. After that convert the result into string using String() initializer and return the resultant string.

import Foundation
import Glibc

func sortString(Str: String) -> String {

    let sortedStr = Str.sorted()
    return String(sortedStr)
}

let enteredString = "ronita sing song"
let resultantString = sortString(Str:enteredString)
print("Original String:", enteredString)
print("Sorted String:", resultantString)

Output

Original String: ronita sing song
Sorted String:   aggiinnnoorsst

Example 2

In the following Swift program, we will sort a string that contains lower and upper-case characters. So create a function which takes the input string as a parameter and returns a sorted string. Inside the function, first, we convert the string into lowercase using the lowercased() function, then the sorted() function to sort all the characters of the string in ascending order. After that convert the result into string using String() initializer and return the resultant string.

import Foundation
import Glibc

func sortString(Str: String) -> String {

   let newStr = Str.lowercased()
   let sortedStr = newStr.sorted()
   return String(sortedStr)
}

let enteredString = "Sohan like iceCream"
let resultantString = sortString(Str:enteredString)
print("Original String:", enteredString)
print("Sorted String:", resultantString)

Output

Original String: Sohan like iceCream
Sorted String:   aacceeehiiklmnors

Example 3

In the following Swift program, we will sort a string in descending order. So create a function which takes the input string as a parameter and returns a sorted string. Inside the function, we will use the sorted(by:>) function with “by:>” parameter to sort all the characters of the string in descending order. After that convert the result into string using String() initializer and return the resultant string.

import Foundation
import Glibc

func sortString(Str: String) -> String {

   let sortedStr = Str.sorted(by:>)
   return String(sortedStr)
}

let enteredString = "machine works fine"
let resultantString = sortString(Str:enteredString)
print("Original String:", enteredString)
print("Sorted String:", resultantString)

Output

Original String: machine works fine
Sorted String: wsronnmkiihfeeca  

Conclusion

So this is how we can sort a string. Here the sorted() method only sorts the string into ascending order. If you want to sort the string into ascending or descending order, then you can use the sorted(by:) function. In ascending order sorting, the precedence of the characters are whitespace characters, special characters, Capital letters, and then small letters.

Updated on: 16-Jun-2023

522 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements