DEV Community

Cover image for Sorting in Go(Golang) made easy
Mathias Jiya
Mathias Jiya

Posted on • Edited on

2 1

Sorting in Go(Golang) made easy

Hello there,
In today's article, I will be talking about https://github.com/iqquee/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"

    "github.com/iqquee/sort"
)

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 := sort.SortInt(arr, 3)
    fmt.Printf("This is the sorted int data: %v\n", sortedInt)

}
Enter fullscreen mode Exit fullscreen mode

SortString()

The SortString() sorts an array of string

package main

import (
    "fmt"

    "github.com/iqquee/sort"
)

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 := sort.SortString(arr, 3)
    fmt.Printf("This is the sorted string data: %v\n", sortedString)

}
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (2)

Collapse
 
marcello_h profile image
Marcelloh

What's the difference between your functions and the standard ones?

Collapse
 
iqquee profile image
Mathias Jiya • Edited

These methods sorts and returns data alongside their number of occurrence with no duplicate values. Also you can specify the number of datas to be returned

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay