config_list.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package tool
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "sort"
  5. "time"
  6. )
  7. type MapsSort struct {
  8. Key string
  9. Type string
  10. Order string
  11. MapList []gin.H
  12. }
  13. func boolToInt(b bool) int {
  14. if b {
  15. return 1
  16. }
  17. return 0
  18. }
  19. func (m MapsSort) Len() int {
  20. return len(m.MapList)
  21. }
  22. func (m MapsSort) Less(i, j int) bool {
  23. flag := false
  24. if m.Type == "int" {
  25. flag = m.MapList[i][m.Key].(int) > m.MapList[j][m.Key].(int)
  26. } else if m.Type == "bool" {
  27. flag = boolToInt(m.MapList[i][m.Key].(bool)) > boolToInt(m.MapList[j][m.Key].(bool))
  28. } else if m.Type == "bool" {
  29. flag = m.MapList[i][m.Key].(string) > m.MapList[j][m.Key].(string)
  30. } else if m.Type == "time" {
  31. flag = m.MapList[i][m.Key].(time.Time).After(m.MapList[j][m.Key].(time.Time))
  32. }
  33. if m.Order == "asc" {
  34. flag = !flag
  35. }
  36. return flag
  37. }
  38. func (m MapsSort) Swap(i, j int) {
  39. m.MapList[i], m.MapList[j] = m.MapList[j], m.MapList[i]
  40. }
  41. func Sort(key string, order string, Type string, maps []gin.H) []gin.H {
  42. mapsSort := MapsSort{
  43. Key: key,
  44. MapList: maps,
  45. Type: Type,
  46. Order: order,
  47. }
  48. sort.Sort(mapsSort)
  49. return mapsSort.MapList
  50. }