sortslice

package module
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 2 Imported by: 0

README

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

MIT License. See LICENSE.


Contributing

Contributions are welcome! To contribute:

  1. Fork the repo on GitHub (using the webpage interface).
  2. Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate to the cloned project (cd repo-name)
  4. Create a feature branch (git checkout -b feature/xxx).
  5. Stage changes (git add .)
  6. Commit changes (git commit -m "Add feature xxx").
  7. Push to the branch (git push origin feature/xxx).
  8. Open a pull request on GitHub (on the GitHub webpage).

Please ensure tests pass and include relevant documentation updates.


Support

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

If you find this package valuable, give me some stars on GitHub! Thank you!!!

Thank you for your support!

Happy Coding with this package! 🎉

Give me stars. Thank you!!!


GitHub Stars

starring

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSortByIndex

func NewSortByIndex[V any](a []V, iLess func(i, j int) bool) sort.Interface

NewSortByIndex creates a new sort.Interface for sorting by index comparison. NewSortByIndex 根据索引比较函数创建一个新的 sort.Interface。

func NewSortByValue

func NewSortByValue[V any](a []V, vLess func(a, b V) bool) sort.Interface

NewSortByValue creates a new sort.Interface for sorting by value comparison. NewSortByValue 根据值比较函数创建一个新的 sort.Interface。

func SortByIndex

func SortByIndex[V any](a []V, iLess func(i, j int) bool)

SortByIndex sorts the slice `a` using the index-based comparison function `iLess`. SortByIndex 使用基于索引的比较函数 `iLess` 对切片 `a` 进行排序。

func SortByValue

func SortByValue[V any](a []V, vLess func(a, b V) bool)

SortByValue sorts the slice `a` using the value-based comparison function `vLess`. SortByValue 使用基于值的比较函数 `vLess` 对切片 `a` 进行排序。

func SortIStable

func SortIStable[V any](a []V, iLess func(i, j int) bool)

SortIStable sorts the slice `a` using the index-based comparison function `iLess` and preserves the original order of equal elements (stable sort). SortIStable 使用基于索引的比较函数 `iLess` 对切片 `a` 进行排序,并保持相等元素的原始顺序(稳定排序)。

func SortVStable

func SortVStable[V any](a []V, vLess func(a, b V) bool)

SortVStable sorts the slice `a` using the value-based comparison function `vLess` and preserves the original order of equal elements (stable sort). SortVStable 使用基于值的比较函数 `vLess` 对切片 `a` 进行排序,并保持相等元素的原始顺序(稳定排序)。

Types

type Slice

type Slice[V any] struct {
	// contains filtered or unexported fields
}

Slice represents a slice that can be sorted using either an index-based or value-based comparison function. Slice 结构体表示一个可以使用索引或值比较函数排序的切片。

func (*Slice[V]) Len

func (T *Slice[V]) Len() int

Len returns the number of elements in the slice. Len 返回切片中的元素数量。

func (*Slice[V]) Less

func (T *Slice[V]) Less(i, j int) bool

Less compares two elements at indexes i and j for sorting. Less 比较索引 i 和 j 处的两个元素,用于排序。

func (*Slice[V]) Swap

func (T *Slice[V]) Swap(i, j int)

Swap swaps the elements with indexes i and j. Swap 交换索引 i 和 j 处的两个元素。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL