Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Edited on

     

Sorting in Go(Golang) made easy

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

What issort ?

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

packagemainimport("fmt""github.com/iqquee/sort")funcmain(){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 datasortedInt:=sort.SortInt(arr,3)fmt.Printf("This is the sorted int data: %v\n",sortedInt)}
Enter fullscreen modeExit fullscreen mode

SortString()

The SortString() sorts an array of string

packagemainimport("fmt""github.com/iqquee/sort")funcmain(){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 datasortedString:=sort.SortString(arr,3)fmt.Printf("This is the sorted string data: %v\n",sortedString)}
Enter fullscreen modeExit fullscreen mode

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
marcello_h profile image
Marcelloh
Go trainer / Principal Go Engineer / Senior Software Architect
  • Location
    The Netherlands
  • Joined

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

CollapseExpand
 
iqquee profile image
Mathias Jiya
A Golang developer to the core. I love building products that are beneficial to people...
  • Email
  • Location
    Ado-Ekiti, Ekiti State, Nigeria
  • Education
    Federal University of Technology, Minna, Niger State, Nigeria.
  • Work
    Backend Engineer @ RebackFinance
  • Joined
• Edited on• 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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A Golang developer to the core. I love building products that are beneficial to people...
  • Location
    Ado-Ekiti, Ekiti State, Nigeria
  • Education
    Federal University of Technology, Minna, Niger State, Nigeria.
  • Work
    Backend Engineer @ RebackFinance
  • Joined

More fromMathias Jiya

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp