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

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

Sorting in Go lang Interface

问题

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

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

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

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

[
    {
        "Career-Business Owner": 0,
        "Career-Entry-level": 0,
        "Dependents-School age children (5-18)": 0,
        "date created": "2021-10-22T13:44:32.655Z",
        "date created": "2021-11-04T05:03:53.805Z",
        "hive_id": 114,
        "name": "Rule test1122-Hive 38",
        "users": 3
    },
    {
        "Career-Business Owner": 0,
        "Career-Entry-level": 0,
        "Dependents-School age children (5-18)": 0,
        "date created": "2021-10-22T13:44:32.655Z",
        "hive_id": 65,
        "name": "Rule hive44555-Hive 8",
        "users": 0
    }
]

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

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

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

if SortBy != "" {
    if SortOrder == "desc" {
        sort.Slice(hives, func(i, j int) bool {
            return hives[i][gpi.SortBy] == hives[j][gpi.SortBy]
        })
    } else {
        sort.Slice(hives, func(i, j int) bool {
            return hives[i][gpi.SortBy] != hives[j][gpi.SortBy]
        })
    }
}

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

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

英文:

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

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

[
        {
             
	   "Career-Business Owner": 0,
            "Career-Entry-level": 0,
            "Dependents-School age children (5-18)": 0,
            "date created": "2021-10-22T13:44:32.655Z",
            "date created": "2021-11-04T05:03:53.805Z",
            "hive_id": 114,
            "name": "Rule test1122-Hive 38",
            "users": 3
        },
        {
            "Career-Business Owner": 0,
            "Career-Entry-level": 0,
            "Dependents-School age children (5-18)": 0,
            "date created": "2021-10-22T13:44:32.655Z",
            "hive_id": 65,
            "name": "Rule hive44555-Hive 8",
            "users": 0
        }
]

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)

 if SortBy != "" {
    		if SortOrder == "desc" {
    			sort.Slice(hives, func(i, j int) bool {
    				return hives[i][gpi.SortBy] == hives[j][gpi.SortBy]
    			})
    		} else {
    			sort.Slice(hives, func(i, j int) bool {
    				return hives[i][gpi.SortBy] != hives[j][gpi.SortBy]
    			})
    		}
    	}

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的方式检查每个字段的类型,并根据找到的类型进行比较。

例如:

sort.Slice(hives, func(i, j int) bool {
    aInt := hives[i][gpi.SortBy]
    bInt := hives[j][gpi.SortBy]
    switch a := aInt.(type) {
    case int:
        if b, ok := bInt.(int); ok {
            return a < b
        }
        panic("无法比较不同类型")

    case string:
        if b, ok := bInt.(string); ok {
            return a < b
        }
        panic("无法比较不同类型")

    default:
        panic("未知类型")

    }
})
英文:

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:

sort.Slice(hives, func(i, j int) bool {
    aInt := hives[i][gpi.SortBy]
    bInt := hives[j][gpi.SortBy]
    switch a := aInt(type) {
    case int:
        if b, ok := bInt.(int); ok {
            return a &lt; b
        }
        panic(&quot;can&#39;t compare dissimilar types&quot;)
        
    case string:
        if b, ok := bInt.(string); ok {
            return a &lt; b
        }
        panic(&quot;can&#39;t compare dissimilar types&quot;)

    default:
        panic(&quot;unknown type&quot;)

    }
})

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:

确定