go特殊字符串排序

2022-11-09  本文已影响0人  小王同学123321
package main

import (
    "errors"
    "fmt"
    "regexp"
    "sort"
    "strconv"
    "strings"
)

func SortStrSliceone(newestslice *[]string)(error){
    //
    slicelen := len(*newestslice)
    if slicelen == 1 {
        return nil
    }
    intpattern := regexp.MustCompile(`\d+`)
    var helpmap = make(map[int]string,slicelen)
    var tmpintslice = make([]int,0)

    for _,eveline := range *newestslice{
        reveslice := intpattern.FindAllString(eveline,-1)
        evestr := strings.Join(reveslice,"")
        eveint,err := strconv.Atoi(evestr)
        if err != nil{
            return errors.New("版本中存在分整数信息")
        }
        fmt.Println(eveint)
        tmpintslice=append(tmpintslice,eveint)
        _,ok := helpmap[eveint];if !ok{
            helpmap[eveint]=eveline
        }
    }
    sort.Ints(tmpintslice)
    fmt.Println(tmpintslice)
    fmt.Println(helpmap)
    return nil
}

func fivearrcompare(arr,brr [5]int)(bool){
    var inncomfunc func(int)(bool)
    inncomfunc = func(num int)(bool){
        if num > 4{
            return false
        }else if num <= 4{
            switch  {
            case arr[num] > brr[num]:
                return true
            case arr[num] < brr[num]:
                return false
            case arr[num] == brr[num]:
                return inncomfunc(num + 1)
            }
        }
        return false
    }
    res := inncomfunc(0)
    return res
}

func SortStrSlicetwo(newestslice *[]string)(error){
    //
    slicelen := len(*newestslice)
    if slicelen == 1 {
        return nil
    }

    var arrmap = make(map[[5]int]string)
    var needslice = make([][5]int,0)
    for _,eveline := range *newestslice{
        var tmparr = [5]int{}
        eveslice := strings.Split(eveline,"_")
        for i:=0;i<len(eveslice);i++{
            linestr,_ := strconv.Atoi(eveslice[i])
            tmparr[i]=linestr
        }
        arrmap[tmparr]=eveline
        needslice=append(needslice,tmparr)
    }
    fmt.Println(needslice)
    fmt.Println(arrmap)
    for i:=0;i<slicelen;i++{
        max := i
        for j:=i+1;j<slicelen;j++{
            if fivearrcompare(needslice[j],needslice[max]){
                max=j
            }
        }
        if max != i{
            needslice[max],needslice[i]=needslice[i],needslice[max]
        }
    }
    fmt.Println(needslice)
    fmt.Printf("max:%s\tmin:%s\n",arrmap[needslice[0]],arrmap[needslice[len(needslice)-1]])
    return nil
}

func main(){
    needsortslice := []string{"100_1","20_3","22_5","34_1","12_1034","10_1001","1010_01","999_100","901_99","999_101","900_100","102_10","10_210","1_3452","102_10_123_345_123","102"}
    SortStrSlicetwo(&needsortslice)
}
上一篇下一篇

猜你喜欢

热点阅读