Skip to content

Commit

Permalink
新增:OrderByThen,自定义多条件,返回true时,排在前面
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 9, 2024
1 parent 31d4284 commit 3b18b35
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions enumerable.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package collections

import (
"github.com/farseer-go/fs/parse"
"github.com/farseer-go/fs/types"
"github.com/timandy/routine"
"math/rand"
"reflect"
"strings"
"sync"
"time"

"github.com/farseer-go/fs/parse"
"github.com/farseer-go/fs/types"
"github.com/timandy/routine"
)

type Enumerable[T any] struct {
Expand Down Expand Up @@ -526,6 +527,38 @@ func (receiver Enumerable[T]) OrderBy(fn func(item T) any) Enumerable[T] {
return Enumerable[T]{source: &lst, lock: &sync.RWMutex{}}
}

// OrderByThen 自定义多条件,返回true时,排在前面
func (receiver Enumerable[T]) OrderByThen(fn func(leftItem, rightItem T) bool) Enumerable[T] {
if receiver.lock == nil {
return receiver
}

receiver.lock.RLock()
defer receiver.lock.RUnlock()

var lst []T
lst = append(lst, *receiver.source...)

// 首先拿数组第0个出来做为左边值
for leftIndex := 0; leftIndex < len(lst); leftIndex++ {
// 再拿出左边值索引后面的值一一对比
for rightIndex := leftIndex + 1; rightIndex < len(lst); rightIndex++ {
rightItem := lst[rightIndex]

// 后面的值比前面的值小,说明要交换数据
if fn(lst[leftIndex], rightItem) {
// 开始交换数据,先从后面交换到前面
for swapIndex := rightIndex; swapIndex > leftIndex; swapIndex-- {
lst[swapIndex] = lst[swapIndex-1]
}
lst[leftIndex] = rightItem
}
}
}

return Enumerable[T]{source: &lst, lock: &sync.RWMutex{}}
}

// OrderByItem 正序排序,fn 返回的是要排序的字段的值
func (receiver Enumerable[T]) OrderByItem() Enumerable[T] {
if receiver.lock == nil {
Expand Down

0 comments on commit 3b18b35

Please sign in to comment.