美文网首页
golang自己实现sort 接口

golang自己实现sort 接口

作者: 上弦月Tt | 来源:发表于2020-08-07 15:29 被阅读0次

最近通过gitlab去获取服务的版本的时候顺序是乱的, 导致运维平台获取最新的10个版本,不是最新的.
元素局大概是这样.

[
   [
    {
        "name":"v0.9.4",
        "commit":{
            "committed_date":"2020-02-28T20:14:27+08:00"
        }
    },
    {
        "name":"v0.11.5",
        "commit":{
            "committed_date":"2020-07-01T17:15:53+08:00"
        }
    },
    {
        "name":"v0.14.6",
        "commit":{
            "committed_date":"2020-08-02T18:20:22+08:00"
        }
    },
    {
        "name":"v0.9.7",
        "commit":{
            "committed_date":"2020-03-25T17:04:17+08:00"
        }
    }
]

我是是按照committed_date 排序, 要想用go 自带sort 排序就得实现sort 接口, 接口规范如下

type Interface interface {
    // Len is the number of elements in the collection.
    Len() int
    // Less reports whether the element with
    // index i should sort before the element with index j.
    Less(i, j int) bool
    // Swap swaps the elements with indexes i and j.
    Swap(i, j int)
}

排序的完整代码如下:

package main

import (
    "fmt"
    "github.com/json-iterator/go"
    "log"
    "sort"
    "time"
)

func main() {
    var tag Tags
    err := jsoniter.Unmarshal([]byte(sourceDatas), &tag)
    if err != nil {
        log.Fatal(err.Error())
        return
    }
    sort.Sort(tag)
    fmt.Printf("排序后的的数据 %s", tag)
}

type Tag struct {
    Name   string `json:"name"`
    Commit struct {
        CommittedDate time.Time `json:"committed_date"`
    } `json:"commit"`
}

// 需要排序的json
var sourceDatas = `[
    {
        "name":"v0.9.4",
        "commit":{
            "committed_date":"2020-02-28T20:14:27+08:00"
        }
    },
    {
        "name":"v0.11.5",
        "commit":{
            "committed_date":"2020-07-01T17:15:53+08:00"
        }
    },
    {
        "name":"v0.14.6",
        "commit":{
            "committed_date":"2020-08-02T18:20:22+08:00"
        }
    },
    {
        "name":"v0.9.7",
        "commit":{
            "committed_date":"2020-03-25T17:04:17+08:00"
        }
    }
]`

type Tags []Tag

func (t Tags) Len() int {
    return len(t)
}

func (t Tags) Less(i, j int) bool {
    ti := t[i].Commit.CommittedDate
    tj := t[j].Commit.CommittedDate
    return ti.Before(tj) // 按照Commit进行升序
}

func (t Tags) Swap(i, j int) {
    t[i], t[j] = t[j], t[i]
}

相关文章

  • golang自己实现sort 接口

    最近通过gitlab去获取服务的版本的时候顺序是乱的, 导致运维平台获取最新的10个版本,不是最新的.元素局大概是...

  • Golang 数据排序

    sort.Interface 接口 这个接口是 sort 包的核心,它有3个方法。这是 Golang 很酷的一个特...

  • Go 在具体编程中的一些实践

    排序 Golang 提供 sort package[https://golang.org/pkg/sort/] 来...

  • Java对象排序

    实现Comparator接口-->Collections.sort(List, new ComparaObject...

  • Go 学习笔记 11 | Golang 接口详解

    一、Golang 接口 Golang 中接口定义了对象的行为规范,只定义规范不实现。接口中定义的规范由具体的对象来...

  • 16-Comparable

    类介绍 一个接口,实现这个后在使用Collections.sort(),Arrays.sort(),SortedM...

  • list.sort排序

    list.sort排序 需要实现comparator接口,重写compare方法,在list.sort中,返回-1...

  • ArrayList中的sort排序

    ArrayList中有一个sort排序方法,只要你实现了Comparator的接口,按照你自己的排序业务进行实现,...

  • Golang 实现接口

    Duck Typing golang的接口很有意思,与java的思路不一样,java中是申明是的,必须使用关键字i...

  • Comparable,Comparator

    comparable接口是一个直接在实例类里实现的接口,实现后就能支持Collections.sort()、Tre...

网友评论

      本文标题:golang自己实现sort 接口

      本文链接:https://www.haomeiwen.com/subject/clbfdktx.html