Sorting in Go(Golang) made easy

Hello there,
In today’s article, I will be talking about https://github.com/hisyntax/sort, a Go library I built for sorting data.

What is sort ?

sort is a Go library that sorts data(both string and int) with their number of occurrences and r…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Mathias Jiya

Hello there,
In today's article, I will be talking about https://github.com/hisyntax/sort, a Go library I built for sorting data.

What is sort ?

sort is a Go library that sorts data(both string and int) with their number of occurrences and returns the desired data length in descending order with no duplicates of values.

Use cases range from:

  • To implement a trending feature in an e-commerce platform
  • To get the most ordered(purchased) products e.t.c

This library contains two methods in the x package:

SortInt()

The SortInt() sorts an array of int

package main

import (
    "fmt"

    sorter "github.com/hisyntax/sort/x"
)

func main() {

    arr := []int{2, 3, 10, 4, 1, 10, 1, 4, 4, 5, 6, 6, 6, 6, 1, 6, 7, 8, 12, 9, 1, 1, 1}

    //pass in the array to be sorted and the desired length of data to be returned in descending order
    //the lenght should be zero(0) if you want to get all the sorted data
    sortedInt := sorter.SortInt(arr, 3)
    fmt.Printf("This is the sorted int data: %v\n", sortedInt)

}

SortString()

The SortString() sorts an array of string

package main

import (
    "fmt"

    sorter "github.com/hisyntax/sort/x"
)

func main() {

    arr := []string{"by", "me", "come", "by", "me", "hello", "hey", "hey", "me", "buy", "by", "come", "hello", "go"}

     //pass in the array to be sorted and the desired length of data to be returned in descending order
 //the lenght should be zero(0) if you want to get all the sorted data
    sortedString := sorter.SortString(arr, 3)
    fmt.Printf("This is the sorted string data: %v\n", sortedString)

}


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Mathias Jiya


Print Share Comment Cite Upload Translate Updates
APA

Mathias Jiya | Sciencx (2022-12-13T19:55:01+00:00) Sorting in Go(Golang) made easy. Retrieved from https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/

MLA
" » Sorting in Go(Golang) made easy." Mathias Jiya | Sciencx - Tuesday December 13, 2022, https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/
HARVARD
Mathias Jiya | Sciencx Tuesday December 13, 2022 » Sorting in Go(Golang) made easy., viewed ,<https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/>
VANCOUVER
Mathias Jiya | Sciencx - » Sorting in Go(Golang) made easy. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/
CHICAGO
" » Sorting in Go(Golang) made easy." Mathias Jiya | Sciencx - Accessed . https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/
IEEE
" » Sorting in Go(Golang) made easy." Mathias Jiya | Sciencx [Online]. Available: https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/. [Accessed: ]
rf:citation
» Sorting in Go(Golang) made easy | Mathias Jiya | Sciencx | https://www.scien.cx/2022/12/13/sorting-in-gogolang-made-easy/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.