在Go语言中进行排序的接口

huangapple go评论122阅读模式
英文:

Sorting in Go lang Interface

问题

我正在使用Go语言。我有一个动态数据创建的需求,所以我使用了一个接口来添加我的数据。

  1. hives := make([]map[string]interface{}, lenHive)

在这个接口中,我添加了一些数据,并且其中一些数据是动态添加的。

在这个接口中,我有以下数据:

  1. [
  2. {
  3. "Career-Business Owner": 0,
  4. "Career-Entry-level": 0,
  5. "Dependents-School age children (5-18)": 0,
  6. "date created": "2021-10-22T13:44:32.655Z",
  7. "date created": "2021-11-04T05:03:53.805Z",
  8. "hive_id": 114,
  9. "name": "Rule test1122-Hive 38",
  10. "users": 3
  11. },
  12. {
  13. "Career-Business Owner": 0,
  14. "Career-Entry-level": 0,
  15. "Dependents-School age children (5-18)": 0,
  16. "date created": "2021-10-22T13:44:32.655Z",
  17. "hive_id": 65,
  18. "name": "Rule hive44555-Hive 8",
  19. "users": 0
  20. }
  21. ]

现在,我需要按照每个字段对这些数据进行排序(需要在每个字段上进行排序)。

我应该如何对接口中的字段进行排序?

这里的SortBy是字段(例如Career-Business OwnerCareer-Entry-leveldate createdhive_idnameusers)。

  1. if SortBy != "" {
  2. if SortOrder == "desc" {
  3. sort.Slice(hives, func(i, j int) bool {
  4. return hives[i][gpi.SortBy] == hives[j][gpi.SortBy]
  5. })
  6. } else {
  7. sort.Slice(hives, func(i, j int) bool {
  8. return hives[i][gpi.SortBy] != hives[j][gpi.SortBy]
  9. })
  10. }
  11. }

但是排序没有正常工作。有没有对接口进行排序的方法?

或者是否存在其他解决这个问题的替代方法?

英文:

I am using Go. I have a dynamic data creation so I used an interface for adding my data.

  1. hives := make([]map[string]interface{}, lenHive)

after some operation in this interface I added some data. In this interface some of the data are dynamically adding.

In this interface I have a data like below

  1. [
  2. {
  3. "Career-Business Owner": 0,
  4. "Career-Entry-level": 0,
  5. "Dependents-School age children (5-18)": 0,
  6. "date created": "2021-10-22T13:44:32.655Z",
  7. "date created": "2021-11-04T05:03:53.805Z",
  8. "hive_id": 114,
  9. "name": "Rule test1122-Hive 38",
  10. "users": 3
  11. },
  12. {
  13. "Career-Business Owner": 0,
  14. "Career-Entry-level": 0,
  15. "Dependents-School age children (5-18)": 0,
  16. "date created": "2021-10-22T13:44:32.655Z",
  17. "hive_id": 65,
  18. "name": "Rule hive44555-Hive 8",
  19. "users": 0
  20. }
  21. ]

now I need to sort this data with each field (need to use sorting in each field)

How can I sort filed from interface

here SortBy is the field (eg Career-Business Owner,Career-Entry-level,date created, hive_id,name,users)

  1. if SortBy != "" {
  2. if SortOrder == "desc" {
  3. sort.Slice(hives, func(i, j int) bool {
  4. return hives[i][gpi.SortBy] == hives[j][gpi.SortBy]
  5. })
  6. } else {
  7. sort.Slice(hives, func(i, j int) bool {
  8. return hives[i][gpi.SortBy] != hives[j][gpi.SortBy]
  9. })
  10. }
  11. }

but the sorting is not working properly. What is the method for sorting interface ?

Or any alternative method exist for solving this?

答案1

得分: 2

你需要提供给sort.Slicefunc应该在索引i处的值小于索引j处的值时返回true。因此,你应该用<替换==!=,或者用>=进行逆向排序。

Go语言没有隐式类型转换,所以在你的less函数中,你需要使用类似type switch的方式检查每个字段的类型,并根据找到的类型进行比较。

例如:

  1. sort.Slice(hives, func(i, j int) bool {
  2. aInt := hives[i][gpi.SortBy]
  3. bInt := hives[j][gpi.SortBy]
  4. switch a := aInt.(type) {
  5. case int:
  6. if b, ok := bInt.(int); ok {
  7. return a < b
  8. }
  9. panic("无法比较不同类型")
  10. case string:
  11. if b, ok := bInt.(string); ok {
  12. return a < b
  13. }
  14. panic("无法比较不同类型")
  15. default:
  16. panic("未知类型")
  17. }
  18. })
英文:

The func you need to supply to sort.Slice should return true if the value at index i is less then the value at index i. So you should replace == and != with &lt;, or with &gt;= for reverse sorting.

Go has no implicit type casing so in your less func you will have to check the type of each field with something like a type switch and handle the comparison based on the type you find.

For example:

  1. sort.Slice(hives, func(i, j int) bool {
  2. aInt := hives[i][gpi.SortBy]
  3. bInt := hives[j][gpi.SortBy]
  4. switch a := aInt(type) {
  5. case int:
  6. if b, ok := bInt.(int); ok {
  7. return a &lt; b
  8. }
  9. panic(&quot;can&#39;t compare dissimilar types&quot;)
  10. case string:
  11. if b, ok := bInt.(string); ok {
  12. return a &lt; b
  13. }
  14. panic(&quot;can&#39;t compare dissimilar types&quot;)
  15. default:
  16. panic(&quot;unknown type&quot;)
  17. }
  18. })

huangapple
  • 本文由 发表于 2021年11月11日 03:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/69918855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定