@@ -3,6 +3,7 @@ package lang
33import (
44 "fmt"
55 "sort"
6+ "strings"
67)
78
89// SortSlice performs an in-place stable sort on the given array using the provided comparator.
@@ -28,7 +29,7 @@ func SortSlice(slice []any, comp any) {
2829 if boolResult , ok := result .(bool ); ok {
2930 return boolResult
3031 }
31-
32+
3233 // Numeric comparator returns:
3334 // -1 if first arg is less than second
3435 // 0 if args are equal
@@ -47,7 +48,12 @@ func SortSlice(slice []any, comp any) {
4748// 'less than', 'equal to', or 'greater than' y.
4849// Handles nil values (nil is less than everything except nil).
4950func Compare (x , y any ) int {
50- // Handle nil cases first
51+ // Identity check
52+ if x == y {
53+ return 0
54+ }
55+
56+ // Handle nil cases
5157 if IsNil (x ) {
5258 if IsNil (y ) {
5359 return 0
@@ -59,41 +65,19 @@ func Compare(x, y any) int {
5965 }
6066
6167 // Handle numbers using the Numbers.Compare method
62- xNum , xIsNum := AsNumber (x )
63- yNum , yIsNum := AsNumber (y )
64- if xIsNum && yIsNum {
65- return Numbers .Compare (xNum , yNum )
68+ if xNum , xIsNum := AsNumber (x ); xIsNum {
69+ return Numbers .Compare (xNum , y )
6670 }
6771
68- // Handle strings
69- if xStr , xOk := x .(string ); xOk {
70- if yStr , yOk := y .(string ); yOk {
71- if xStr < yStr {
72- return - 1
73- } else if xStr > yStr {
74- return 1
75- }
76- return 0
77- }
78- }
79-
80- // Handle keywords
81- if xKw , xOk := x .(Keyword ); xOk {
82- if yKw , yOk := y .(Keyword ); yOk {
83- return Compare (xKw .String (), yKw .String ())
84- }
72+ // Check if x implements Comparer interface
73+ if xComp , ok := x .(Comparer ); ok {
74+ return xComp .Compare (y )
8575 }
8676
87- // Handle symbols
88- if xSym , xOk := x .(* Symbol ); xOk {
89- if ySym , yOk := y .(* Symbol ); yOk {
90- // Compare namespace first
91- nsComp := Compare (xSym .Namespace (), ySym .Namespace ())
92- if nsComp != 0 {
93- return nsComp
94- }
95- // Then compare name
96- return Compare (xSym .Name (), ySym .Name ())
77+ // Handle strings (built-in type, doesn't implement Comparer)
78+ if xStr , xOk := x .(string ); xOk {
79+ if yStr , yOk := y .(string ); yOk {
80+ return strings .Compare (xStr , yStr )
9781 }
9882 }
9983
@@ -109,32 +93,6 @@ func Compare(x, y any) int {
10993 }
11094 }
11195
112- // Handle vectors (including MapEntry which is a vector)
113- if xVec , xOk := x .(IPersistentVector ); xOk {
114- if yVec , yOk := y .(IPersistentVector ); yOk {
115- xCount := xVec .Count ()
116- yCount := yVec .Count ()
117- minCount := xCount
118- if yCount < minCount {
119- minCount = yCount
120- }
121- // Compare element by element
122- for i := 0 ; i < minCount ; i ++ {
123- cmp := Compare (xVec .Nth (i ), yVec .Nth (i ))
124- if cmp != 0 {
125- return cmp
126- }
127- }
128- // If all compared elements are equal, shorter vector is less
129- if xCount < yCount {
130- return - 1
131- } else if xCount > yCount {
132- return 1
133- }
134- return 0
135- }
136- }
137-
138- // If we can't compare, panic with an error
139- panic (NewIllegalArgumentError (fmt .Sprintf ("Cannot compare %T with %T" , x , y )))
96+ // Default error - cannot compare
97+ panic (NewIllegalArgumentError (fmt .Sprintf ("%T cannot be cast to Comparable" , x )))
14098}
0 commit comments