doudu2515 2013-08-21 10:46
浏览 432
已采纳

Go中的字母数字排序

I am reading rows from the GAE Datastore and I want to sort them alphanumerically.

Suppose I have something like this:

key      name      description    sequence  
===========================================  
ASD..    maths1    it is maths    chap21.1  
ASD..    maths2    it is maths    chap21.10  
ASD..    maths3    it is maths    chap21.2  

I want the result sorted alphanumerically on the sequence field, like this:

key      name      description    sequence  
===========================================  
ASD..    maths1    it is maths    chap21.1  
ASD..    maths3    it is maths    chap21.2 
ASD..    maths2    it is maths    chap21.10   
  • 写回答

4条回答 默认 最新

  • doupu1949 2013-08-21 21:06
    关注

    Use ISO/IEC 14651:2011 to construct the sequence sort key. For example,

    package main
    
    import (
        "fmt"
        "sort"
    )
    
    const maxByte = 1<<8 - 1
    
    func isDigit(d byte) bool {
        return '0' <= d && d <= '9'
    }
    
    func SequenceKey(key string) string {
        sKey := make([]byte, 0, len(key)+8)
        j := -1
        for i := 0; i < len(key); i++ {
            b := key[i]
            if !isDigit(b) {
                sKey = append(sKey, b)
                j = -1
                continue
            }
            if j == -1 {
                sKey = append(sKey, 0x00)
                j = len(sKey) - 1
            }
            if sKey[j] == 1 && sKey[j+1] == '0' {
                sKey[j+1] = b
                continue
            }
            if sKey[j]+1 > maxByte {
                panic("SequenceKey: invalid key")
            }
            sKey = append(sKey, b)
            sKey[j]++
        }
        return string(sKey)
    }
    
    type Chapter struct {
        Key         string
        Name        string
        Description string
        Sequence    string
        SequenceKey string `datastore:"-"`
    }
    
    type Chapters []*Chapter
    
    var chapters = Chapters{
        {Key: "ASD..", Name: "maths1", Description: "it is maths", Sequence: "chap21.1"},
        {Key: "ASD..", Name: "maths2", Description: "it is maths", Sequence: "chap21.10"},
        {Key: "ASD..", Name: "maths3", Description: "it is maths", Sequence: "chap21.2"},
    }
    
    func (s Chapters) Len() int {
        return len(s)
    }
    
    func (s Chapters) Swap(i, j int) {
        s[i], s[j] = s[j], s[i]
    }
    
    type BySequenceKey struct{ Chapters }
    
    func (s BySequenceKey) Less(i, j int) bool {
        return s.Chapters[i].SequenceKey < s.Chapters[j].SequenceKey
    }
    
    func main() {
        for _, chapter := range chapters {
            chapter.SequenceKey = SequenceKey(chapter.Sequence)
        }
        fmt.Println("Unsorted:")
        for _, chapter := range chapters {
            fmt.Printf("  sequence: %#v
    ", chapter.Sequence)
            fmt.Printf("    sort key: %#v
    ", chapter.SequenceKey)
            fmt.Printf("      name: %#v
    ", chapter.Name)
        }
        fmt.Println("Sorted:")
        sort.Sort(BySequenceKey{chapters})
        for _, chapter := range chapters {
            fmt.Printf("  sequence: %#v
    ", chapter.Sequence)
            fmt.Printf("    sort key: %#v
    ", chapter.SequenceKey)
            fmt.Printf("      name: %#v
    ", chapter.Name)
        }
    }
    

    Output:

    Unsorted:
      sequence: "chap21.1"
        sort key: "chap\x0221.\x011"
          name: "maths1"
      sequence: "chap21.10"
        sort key: "chap\x0221.\x0210"
          name: "maths2"
      sequence: "chap21.2"
        sort key: "chap\x0221.\x012"
          name: "maths3"
    Sorted:
      sequence: "chap21.1"
        sort key: "chap\x0221.\x011"
          name: "maths1"
      sequence: "chap21.2"
        sort key: "chap\x0221.\x012"
          name: "maths3"
      sequence: "chap21.10"
        sort key: "chap\x0221.\x0210"
          name: "maths2"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程