44 "reflect"
55 "sort"
66 "time"
7+ "unicode"
78
89 storageTY "github.com/mycontroller-org/server/v2/plugin/database/storage/types"
910)
@@ -40,6 +41,46 @@ func Sort(entities []interface{}, pagination *storageTY.Pagination) ([]interface
4041 return entities , entitiesCount
4142}
4243
44+ // naturalStringLess compares strings using natural sort order
45+ // Numbers within strings are compared numerically
46+ func naturalStringLess (a , b string ) bool {
47+ aRunes := []rune (a )
48+ bRunes := []rune (b )
49+ i , j := 0 , 0
50+
51+ for i < len (aRunes ) && j < len (bRunes ) {
52+ // Check if both are digits
53+ if unicode .IsDigit (aRunes [i ]) && unicode .IsDigit (bRunes [j ]) {
54+ // Extract numbers
55+ aNum , aEnd := extractNumber (aRunes , i )
56+ bNum , bEnd := extractNumber (bRunes , j )
57+
58+ if aNum != bNum {
59+ return aNum < bNum
60+ }
61+ i = aEnd
62+ j = bEnd
63+ } else {
64+ if aRunes [i ] != bRunes [j ] {
65+ return aRunes [i ] < bRunes [j ]
66+ }
67+ i ++
68+ j ++
69+ }
70+ }
71+ return len (aRunes ) < len (bRunes )
72+ }
73+
74+ // extractNumber extracts a number from rune slice starting at pos
75+ func extractNumber (runes []rune , pos int ) (int , int ) {
76+ num := 0
77+ for pos < len (runes ) && unicode .IsDigit (runes [pos ]) {
78+ num = num * 10 + int (runes [pos ]- '0' )
79+ pos ++
80+ }
81+ return num , pos
82+ }
83+
4384// GetSortByKeyPath returns the slice in order
4485func GetSortByKeyPath (keyPath , orderBy string , data []interface {}) []interface {} {
4586 sort .Slice (data , func (a , b int ) bool {
@@ -64,9 +105,9 @@ func GetSortByKeyPath(keyPath, orderBy string, data []interface{}) []interface{}
64105 return false
65106 }
66107 if orderBy == storageTY .SortByASC {
67- return aFinalValue < bFinalValue
108+ return naturalStringLess ( aFinalValue , bFinalValue )
68109 }
69- return aFinalValue > bFinalValue
110+ return naturalStringLess ( bFinalValue , aFinalValue )
70111
71112 case reflect .Int :
72113 aFinalValue , aOK := aValue .(int )
0 commit comments