Share

# Sort String In Ascending Order

Home Â» Programming Language Â» Golang Â» Sort String In Ascending Order

In this article we are going to learn about how we can Sort String In Ascending Order using Golang Programming Language. If you are not familiar what ascending and descending order is let me explain you about it or if you knows then you can skip that part.

# Sort String In Ascending Order?

Let’s assume we have string which is ‘fedcba’ but the order is request in alphabetical which is ‘abcdef’, So for achieving this we need a function which will order any unordered string.

### What is Ascending Order?

Ascending order is a method of arranging numbers from smallest value to largest value. The order goes from left to right. Ascending order is also sometimes named as increasing order. For example, a set of natural numbers are in ascending order, such as 1, 2, 3, 4, 5, 6, 7, 8â€¦ and same for the alphabets as well. The inverse method of increasing order is descending order, where the numbers are arranged in decreasing order of values.

### What is Descending Order?

If the information is sorted from highest to lowest, it is said to be in descending order. For example 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 are arranged in descending order. In other words, if the numbers are arranged from the largest to the smallest number, it is said to be in descending order. In simple words, descending order is defined as an arrangement in the highest to lowest format. These concepts are related to decimals, numbers, fractions or amount of money. Example of Descending Order 24, 20, 18, 12, 7 are arranged in descending order. This is also known as decreasing order of numbers, So now we knows what is ascending and descending order then let’s move forward main part which is runes.

### What is Rune in Golang?

Rune literals are just 32-bit integer values (however they’re untyped constants, so their type can change). They represent Unicode codepoints. For example, the rune literal `'a'` is actually the number `97`.We have covered all reading part which was important while we were going to code, So let’s start writing code.

### 1. Converting String To Slice of Rune

``````
str := "fedcba"
runes := []rune(str)

``````

So we’ve a str variable which have string type and then to get the indexes of Unicode we’ve used slice of rune.

### 2. Sorting The Rune Values

``````
func alphasort(str []rune, depth int) {
for x := range str {
y := x + 1
for y = range str {
if str[x] < str[y] {
str[x], str[y] = str[y], str[x]
}
}
}
}
``````

In alphasort function we are using bubble sort and sorting the slice of rune via numbers because we can’t directly compare characters which is bigger or not.

### 3. Converting Rune Values Back To String

``````
func runeToCharacter(r []rune) string {
str := ""
for _, c := range r {
str += fmt.Sprintf("%c", c)
}
return str
}
``````

After getting our sorted slice of rune we’re getting a properly ready string and also converting rune value back to string characters with the help of loop, if you’re trying to convert it without help of loop you’ll gonna get the error.

## Highlights

March 20, 2023

March 17, 2023

March 14, 2023

March 10, 2023

March 1, 2023

### Differences between sync.Map and map in Go

February 27, 2023

### Build a To-Do App with Local Storage using Vanilla JavaScript

February 24, 2023