DEV Community

Cover image for [Golang] Sorting String In Alphabetical Order
kuldeep_singh
kuldeep_singh

Posted on

[Golang] Sorting String In Alphabetical Order

In this article we are going to learn about how we can sort strings in golang the order we are going to implement is ascending order. 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.

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.

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]
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Checkout Here for more methods on bubble sort implementation:
https://kdsingh4.blogspot.com/2021/10/golang-sort-string-in-alphabetical-order.html

Checkout More Algorithms Solutions: https://kdsingh4.blogspot.com/search/label/algorithm?&max-results=5/feed/

Don't Forget To Subscribe My Youtube Channel as well:
https://www.youtube.com/channel/UCXhmNqWQ50zitqIdTgS7s8Q

Discussion (0)