@@ -3,28 +3,30 @@ package underscore
33import "reflect"
44
55type sorter struct {
6- KeysValue reflect.Value
6+ KeysValues [] reflect.Value
77 ValuesValue reflect.Value
88}
99
1010func (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
1818func (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
3638func (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