Skip to content

`sortslice` is a Go package that provides a simple and flexible way to sort slices using custom comparison functions.

License

Notifications You must be signed in to change notification settings

yyle88/sortslice

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

sortslice

sortslice is a Go package that provides a simple and flexible way to sort slices using custom comparison functions. It leverages Go's generics and the sort.Interface to avoid repeating the implementation of sorting logic for different types.

CHINESE README

中文说明

Installation

To install the sortslice package, you can use the following command:

go get github.com/yyle88/sortslice

Usage

The package offers several functions for sorting slices with different comparison strategies. Below are the key functions available:

SortByIndex

Sorts the slice a using an index-based comparison function iLess.

sortslice.SortByIndex(a []V, iLess func(i, j int) bool)
  • a: The slice to be sorted.
  • iLess: The function that compares the indices of two elements in the slice.
  • Sorts the slice in place using the provided index-based comparison function.

SortByValue

Sorts the slice a using a value-based comparison function vLess.

sortslice.SortByValue(a []V, vLess func(a, b V) bool)
  • a: The slice to be sorted.
  • vLess: The function that compares the values of two elements in the slice.
  • Sorts the slice in place using the provided value-based comparison function.

SortIStable

Sorts the slice a using an index-based comparison function iLess and preserves the order of equal elements (stable sort).

sortslice.SortIStable(a []V, iLess func(i, j int) bool)
  • a: The slice to be sorted.
  • iLess: The function that compares the indices of two elements in the slice.
  • Sorts the slice in place while maintaining the original order of equal elements (stable sort).

SortVStable

Sorts the slice a using a value-based comparison function vLess and preserves the order of equal elements (stable sort).

sortslice.SortVStable(a []V, vLess func(a, b V) bool)
  • a: The slice to be sorted.
  • vLess: The function that compares the values of two elements in the slice.
  • Sorts the slice in place while maintaining the original order of equal elements (stable sort).

Example

Here's a basic example of how to use SortByIndex and SortByValue:

package main

import (
	"fmt"
	"github.com/yyle88/sortslice"
)

func main() {
	// Example 1: Sorting by index
	numbers := []int{5, 3, 8, 1, 4}
	sortslice.SortByIndex(numbers, func(i, j int) bool {
		return numbers[i] < numbers[j] // Compare by values at indices
	})
	fmt.Println("Sorted by index:", numbers)

	// Example 2: Sorting by value
	strings := []string{"apple", "banana", "cherry", "date"}
	sortslice.SortByValue(strings, func(a, b string) bool {
		return a < b // Compare by string values
	})
	fmt.Println("Sorted by value:", strings)
}

License

sortslice is open-source and released under the MIT License. See the LICENSE file for more information.


Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!

Happy Coding with sortslice! 🎉

Give me stars. Thank you!!!

See stars

see stars

About

`sortslice` is a Go package that provides a simple and flexible way to sort slices using custom comparison functions.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published