Skip to content

Commit 4c8d632

Browse files
author
ahl5esoft
committed
CN.1875084792839892:699d29e5c3139b2c78a122f205653347_692eb5bd4e3c2243f51002eb.692eb9124e3c2243f5100431.692eb9124901274f8a90ef36:Trae CN.T(2025/12/2 18:01:54)
1 parent ac964c0 commit 4c8d632

File tree

3 files changed

+85
-47
lines changed

3 files changed

+85
-47
lines changed

i-enumerable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type IEnumerable interface {
3232
MapMany(selector interface{}) IEnumerable
3333
MapManyBy(fieldName string) IEnumerable
3434
Object() IEnumerable
35-
Order(selector interface{}) IEnumerable
36-
OrderBy(fieldName string) IEnumerable
35+
Order(selectors ...interface{}) IEnumerable
36+
OrderBy(fieldNames ...string) IEnumerable
3737
Reduce(fn interface{}, memo interface{}) IEnumerable
3838
Reject(predicate interface{}) IEnumerable
3939
RejectBy(fields map[string]interface{}) IEnumerable

order.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@ package underscore
22

33
import "sort"
44

5-
func (m enumerable) Order(selector interface{}) IEnumerable {
5+
func (m enumerable) Order(selectors ...interface{}) IEnumerable {
66
return enumerable{
77
Enumerator: func() IEnumerator {
88
s := new(sorter)
99
s.Sort(
1010
m.GetEnumerator(),
11-
selector,
11+
selectors...,
1212
)
1313
sort.Sort(s)
1414
return chainFromValue(s.ValuesValue).GetEnumerator()
1515
},
1616
}
1717
}
1818

19-
func (m enumerable) OrderBy(fieldName string) IEnumerable {
20-
getter := FieldValue(fieldName)
21-
return m.Order(func(value, _ interface{}) facade {
22-
return facade{
23-
getter(value),
19+
func (m enumerable) OrderBy(fieldNames ...string) IEnumerable {
20+
selectors := make([]interface{}, len(fieldNames))
21+
for i, fieldName := range fieldNames {
22+
getter := FieldValue(fieldName)
23+
selectors[i] = func(value, _ interface{}) facade {
24+
return facade{
25+
getter(value),
26+
}
2427
}
25-
})
28+
}
29+
return m.Order(selectors...)
2630
}

sorter.go

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@ package underscore
33
import "reflect"
44

55
type sorter struct {
6-
KeysValue reflect.Value
6+
KeysValues []reflect.Value
77
ValuesValue reflect.Value
88
}
99

1010
func (m sorter) Len() int {
11-
if m.KeysValue.IsValid() {
12-
return m.KeysValue.Len()
11+
if len(m.KeysValues) > 0 && m.KeysValues[0].IsValid() {
12+
return m.KeysValues[0].Len()
1313
}
1414

1515
return 0
1616
}
1717

1818
func (m sorter) Swap(i, j int) {
19-
temp := m.KeysValue.Index(i).Interface()
20-
m.KeysValue.Index(i).Set(
21-
m.KeysValue.Index(j),
22-
)
23-
m.KeysValue.Index(j).Set(
24-
reflect.ValueOf(temp),
25-
)
19+
for _, keysValue := range m.KeysValues {
20+
temp := keysValue.Index(i).Interface()
21+
keysValue.Index(i).Set(
22+
keysValue.Index(j),
23+
)
24+
keysValue.Index(j).Set(
25+
reflect.ValueOf(temp),
26+
)
27+
}
2628

27-
temp = m.ValuesValue.Index(i).Interface()
29+
temp := m.ValuesValue.Index(i).Interface()
2830
m.ValuesValue.Index(i).Set(
2931
m.ValuesValue.Index(j),
3032
)
@@ -34,39 +36,71 @@ func (m sorter) Swap(i, j int) {
3436
}
3537

3638
func (m sorter) Less(i, j int) bool {
37-
thisRV := m.KeysValue.Index(i)
38-
thatRV := m.KeysValue.Index(j)
39-
switch thisRV.Kind() {
40-
case reflect.Float32, reflect.Float64:
41-
return thisRV.Float() < thatRV.Float()
42-
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
43-
return thisRV.Int() < thatRV.Int()
44-
case reflect.String:
45-
return thisRV.String() < thatRV.String()
46-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
47-
return thisRV.Uint() < thatRV.Uint()
48-
default:
49-
return false
39+
for _, keysValue := range m.KeysValues {
40+
thisRV := keysValue.Index(i)
41+
thatRV := keysValue.Index(j)
42+
43+
var less bool
44+
equal := false
45+
46+
switch thisRV.Kind() {
47+
case reflect.Float32, reflect.Float64:
48+
thisVal := thisRV.Float()
49+
thatVal := thatRV.Float()
50+
less = thisVal < thatVal
51+
equal = thisVal == thatVal
52+
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
53+
thisVal := thisRV.Int()
54+
thatVal := thatRV.Int()
55+
less = thisVal < thatVal
56+
equal = thisVal == thatVal
57+
case reflect.String:
58+
thisVal := thisRV.String()
59+
thatVal := thatRV.String()
60+
less = thisVal < thatVal
61+
equal = thisVal == thatVal
62+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
63+
thisVal := thisRV.Uint()
64+
thatVal := thatRV.Uint()
65+
less = thisVal < thatVal
66+
equal = thisVal == thatVal
67+
default:
68+
return false
69+
}
70+
71+
if !equal {
72+
return less
73+
}
5074
}
75+
76+
// All fields are equal
77+
return false
5178
}
5279

53-
func (m *sorter) Sort(iterator IEnumerator, selector interface{}) {
54-
selectorValue := reflect.ValueOf(selector)
80+
func (m *sorter) Sort(iterator IEnumerator, selectors ...interface{}) {
81+
selectorValues := make([]reflect.Value, len(selectors))
82+
for i, selector := range selectors {
83+
selectorValues[i] = reflect.ValueOf(selector)
84+
}
85+
5586
for ok := iterator.MoveNext(); ok; ok = iterator.MoveNext() {
56-
keyValue := getReturnValue(selectorValue, iterator)
5787
if m.Len() == 0 {
58-
keysType := reflect.SliceOf(
59-
keyValue.Type(),
60-
)
61-
m.KeysValue = reflect.MakeSlice(keysType, 0, 0)
62-
63-
valuesType := reflect.SliceOf(
64-
iterator.GetValue().Type(),
65-
)
88+
m.KeysValues = make([]reflect.Value, len(selectors))
89+
for i, selectorValue := range selectorValues {
90+
keyValue := getReturnValue(selectorValue, iterator)
91+
keysType := reflect.SliceOf(keyValue.Type())
92+
m.KeysValues[i] = reflect.MakeSlice(keysType, 0, 0)
93+
}
94+
95+
valuesType := reflect.SliceOf(iterator.GetValue().Type())
6696
m.ValuesValue = reflect.MakeSlice(valuesType, 0, 0)
6797
}
68-
69-
m.KeysValue = reflect.Append(m.KeysValue, keyValue)
98+
99+
for i, selectorValue := range selectorValues {
100+
keyValue := getReturnValue(selectorValue, iterator)
101+
m.KeysValues[i] = reflect.Append(m.KeysValues[i], keyValue)
102+
}
103+
70104
m.ValuesValue = reflect.Append(
71105
m.ValuesValue,
72106
iterator.GetValue(),

0 commit comments

Comments
 (0)