Go 自定义排序示例

背景

go提供了几种内置类型的排序函数,例如 sort.Strings、 sort.Float64、 sort.Ints, 但是我们在业务中需要根据业务属性进行排序例如,某个struct中age, createTime 或者priority等等。 本文参考https://gobyexample.com/sorting-by-functions, 给出一个简单自定义排序示例

实现

本示例中字符串是固定格式的{正整数}-{字符串}, 排序只和正整数相关,与后面的字符串无关,因此需要先提取其中的正整数,然后转为数字,最后再比较。

package main

import (
    "fmt"
    "sort"
	"k8s.io/klog"
	"strings"
	"strconv"
)

type byPrefix []string

func (s byPrefix) Len() int {
	return len(s)
}

func GetPrefix(s string) int {
	subStrs := strings.Split(s, "-")
	if len(subStrs) != 0 {
		numStr := subStrs[0]
		i, err := strconv.Atoi(numStr)
        if err != nil {
			klog.Infof("faild to conver to int from %q, err:%v", numStr, s, err)
		} else {
			return i
		} 
	} 

	return -1
}

func (s byPrefix) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s byPrefix) Less(i, j int) bool {
    return GetPrefix(s[i]) < GetPrefix(s[j])
}

func main() {

    strs := []string{"1-create", "2-changeClass", "3-changePara", 
	"10-resart","11-changePara","20-switch", "21-restart", "30-switch", "31-changePara",
	"100-changeClass", "101-changePara",  "110-changePara", 
	"120-openProxy", "121-changeClass", "130-swith", "131-changePara"}
	//按照默认string排序100-changeClass会排在2-changeClass前面,这不是期望的结果
	
	klog.Info("---默认字符串排序---")
    sort.Strings(strs)
    fmt.Println("Strings:", strs)

	klog.Info("---自定义排序---")
	sort.Sort(byPrefix(strs))
	fmt.Println("Strings:", strs)

    ints := []int{1, 2, 3,
	10, 11, 20, 21, 30, 31,
    100, 101, 110, 120, 121, 130, 131}
    sort.Ints(ints)
    fmt.Println("Ints:   ", ints)

    s := sort.IntsAreSorted(ints)
    fmt.Println("Sorted: ", s)
}


其中byPrefix实现了https://pkg.go.dev/sort?utm_source=gopls#Sort中的 sort.Interface - Len, Less, and Swap 方法

type Interface interface {
	// Len is the number of elements in the collection.
	Len() int

	// Less reports whether the element with index i
	// must sort before the element with index j.
	//
	// If both Less(i, j) and Less(j, i) are false,
	// then the elements at index i and j are considered equal.
	// Sort may place equal elements in any order in the final result,
	// while Stable preserves the original input order of equal elements.
	//
	// Less must describe a transitive ordering:
	//  - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
	//  - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.
	//
	// Note that floating-point comparison (the < operator on float32 or float64 values)
	// is not a transitive ordering when not-a-number (NaN) values are involved.
	// See Float64Slice.Less for a correct implementation for floating-point values.
	Less(i, j int) bool

	// Swap swaps the elements with indexes i and j.
	Swap(i, j int)
}

演示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值