@@ -128,6 +128,7 @@ func (f *FieldSetType) addField(field fields.FieldInterface) *FieldSetType {
128
128
//Sort("field1:1,field2:2") or Sort("field1:1","field2:2")
129
129
func (f * FieldSetType ) Sort (sortList ... string ) * FieldSetType {
130
130
size := len (f .fields )
131
+ var endIdx int = size - 1
131
132
var sortSlice []string
132
133
if len (sortList ) == 1 {
133
134
sortSlice = strings .Split (sortList [0 ], "," )
@@ -140,18 +141,21 @@ func (f *FieldSetType) Sort(sortList ...string) *FieldSetType {
140
141
fieldName := ni [0 ]
141
142
if len (ni ) > 1 {
142
143
if ni [1 ] == "last" {
143
- index = size - 1
144
+ index = endIdx
144
145
} else if idx , err := strconv .Atoi (ni [1 ]); err != nil {
145
146
continue
146
147
} else {
147
- index = idx
148
+ if idx >= 0 {
149
+ index = idx
150
+ } else {
151
+ index = endIdx + idx
152
+ }
153
+
148
154
}
149
155
}
150
156
if oldIndex , ok := f .fieldMap [fieldName ]; ok {
151
157
if oldIndex != index && size > index {
152
- f .fields [oldIndex ], f .fields [index ] = f .fields [index ], f .fields [oldIndex ]
153
- f .fieldMap [f .fields [index ].Name ()] = index
154
- f .fieldMap [f .fields [oldIndex ].Name ()] = oldIndex
158
+ f .sortFields (index , oldIndex , endIdx , size )
155
159
}
156
160
}
157
161
index ++
@@ -161,14 +165,13 @@ func (f *FieldSetType) Sort(sortList ...string) *FieldSetType {
161
165
162
166
func (f * FieldSetType ) Sort2Last (fieldsName ... string ) * FieldSetType {
163
167
size := len (f .fields )
164
- var index int = size - 1
168
+ var endIdx int = size - 1
169
+ var index int = endIdx
165
170
for n := len (fieldsName ) - 1 ; n >= 0 ; n -- {
166
171
fieldName := fieldsName [n ]
167
172
if oldIndex , ok := f .fieldMap [fieldName ]; ok {
168
173
if oldIndex != index && index >= 0 {
169
- f .fields [oldIndex ], f .fields [index ] = f .fields [index ], f .fields [oldIndex ]
170
- f .fieldMap [f .fields [index ].Name ()] = index
171
- f .fieldMap [f .fields [oldIndex ].Name ()] = oldIndex
174
+ f .sortFields (index , oldIndex , endIdx , size )
172
175
}
173
176
}
174
177
index --
@@ -225,3 +228,42 @@ func (f *FieldSetType) Enable() *FieldSetType {
225
228
f .RemoveTag ("disabled" )
226
229
return f
227
230
}
231
+
232
+ func (f * FieldSetType ) sortFields (index , oldIndex , endIdx , size int ) {
233
+
234
+ var newFields []fields.FieldInterface = make ([]fields.FieldInterface , 0 )
235
+ var oldFields []fields.FieldInterface = make ([]fields.FieldInterface , size )
236
+ copy (oldFields , f .fields )
237
+ var min , max int
238
+ if index > oldIndex {
239
+ //[ ][I][ ][ ][ ][ ] I:oldIndex=1
240
+ //[ ][ ][ ][ ][I][ ] I:index=4
241
+ if oldIndex > 0 {
242
+ newFields = oldFields [0 :oldIndex ]
243
+ }
244
+ newFields = append (newFields , oldFields [oldIndex + 1 :index + 1 ]... )
245
+ newFields = append (newFields , f .fields [oldIndex ])
246
+ if index + 1 <= endIdx {
247
+ newFields = append (newFields , f .fields [index + 1 :]... )
248
+ }
249
+ min = oldIndex
250
+ max = index
251
+ } else {
252
+ //[ ][ ][ ][ ][I][ ] I:oldIndex=4
253
+ //[ ][I][ ][ ][ ][ ] I:index=1
254
+ if index > 0 {
255
+ newFields = oldFields [0 :index ]
256
+ }
257
+ newFields = append (newFields , oldFields [oldIndex ])
258
+ newFields = append (newFields , f .fields [index :oldIndex ]... )
259
+ if oldIndex + 1 <= endIdx {
260
+ newFields = append (newFields , f .fields [oldIndex + 1 :]... )
261
+ }
262
+ min = index
263
+ max = oldIndex
264
+ }
265
+ for i := min ; i <= max ; i ++ {
266
+ f .fieldMap [newFields [i ].Name ()] = i
267
+ }
268
+ f .fields = newFields
269
+ }
0 commit comments