sorter

package
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultIndexGap 默认稀疏索引间隔
	DefaultIndexGap = 1000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ReorderPlan

type ReorderPlan[T SortItem] struct {
	// NeedRebalance 是否需要重平衡
	NeedRebalance bool
	// NewSortKey 单个元素的新 SortKey(快速路径)
	NewSortKey int64
	// Items 批量更新的元素列表(慢路径)
	Items []T
}

ReorderPlan 重排执行计划 NOTE: T 为排序项类型,必须满足 SortItem 约束

type SortItem

type SortItem interface {
	any
}

SortItem 排序更新项接口 NOTE: 用于批量更新时的数据传输

type Sortable

type Sortable interface {
	GetID() int64
	GetSortKey() int64
}

Sortable 可排序元素接口 NOTE: 所有需要使用拖拽排序功能的实体都应实现此接口

type Sorter

type Sorter[E Sortable, T SortItem] struct {
	// contains filtered or unexported fields
}

Sorter 通用排序器 NOTE: E 为元素类型(实现 Sortable), T 为排序项类型(实现 SortItem)

func NewSorter

func NewSorter[E Sortable, T SortItem](convertFunc func(elem E, idx int) T) *Sorter[E, T]

NewSorter 创建排序器 convertFunc: 将元素转换为排序项,用于重平衡场景 例如: func(attr Attribute, idx int) AttributeSortItem { ... }

func (*Sorter[E, T]) PlanReorder

func (s *Sorter[E, T]) PlanReorder(elements []E, draggedElem E, targetPosition int64) ReorderPlan[T]

PlanReorder 计算重排方案(核心算法,纯函数) elements: 目标分组/列表中的所有元素 draggedElem: 被拖拽的元素 targetPosition: 目标位置 (0-based)

func (*Sorter[E, T]) WithIndexGap

func (s *Sorter[E, T]) WithIndexGap(gap int64) *Sorter[E, T]

WithIndexGap 设置索引间隔

Jump to

Keyboard shortcuts

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