patrickmn / sortutil

Nested, case-insensitive, and reverse sorting for Go
https://patrickmn.com/projects/sortutil/
MIT License
72 stars 16 forks source link

AscByIndex() must run (n-1) times before [][]string is properly sorted #3

Open astockwell opened 8 years ago

astockwell commented 8 years ago

Please see the example below. This also affects CiAscByIndex(), and likely others as well.

Not sure if this is related to #2 as well..?

package main

import (
    "fmt"

    "github.com/patrickmn/sortutil"
)

func main() {
    ss := [][]string{
        {"aaa", "ddd", "ccc", "bbb"},
        {"www", "zzz", "yyy", "xxx"},
        {"lll", "ooo", "nnn", "mmm"},
        {"qqq", "ttt", "sss", "rrr"},
        {"ggg", "jjj", "iii", "hhh"},
        {"ttt", "www", "vvv", "uuu"},
    }
    fmt.Println(ss)
    // [[aaa ddd ccc bbb] [www zzz yyy xxx] [lll ooo nnn mmm] [qqq ttt sss rrr] [ggg jjj iii hhh] [ttt www vvv uuu]]

    sortutil.AscByIndex(ss, 0)
    fmt.Println(ss)
    // [[aaa ddd ccc bbb] [lll ooo nnn mmm] [www zzz yyy xxx] [ggg jjj iii hhh] [qqq ttt sss rrr] [ttt www vvv uuu]]

    sortutil.AscByIndex(ss, 0)
    fmt.Println(ss)
    // [[aaa ddd ccc bbb] [lll ooo nnn mmm] [ggg jjj iii hhh] [www zzz yyy xxx] [qqq ttt sss rrr] [ttt www vvv uuu]]

    sortutil.AscByIndex(ss, 0)
    fmt.Println(ss)
    // [[aaa ddd ccc bbb] [ggg jjj iii hhh] [lll ooo nnn mmm] [qqq ttt sss rrr] [www zzz yyy xxx] [ttt www vvv uuu]]

    sortutil.AscByIndex(ss, 0)
    fmt.Println(ss)
    // [[aaa ddd ccc bbb] [ggg jjj iii hhh] [lll ooo nnn mmm] [qqq ttt sss rrr] [ttt www vvv uuu] [www zzz yyy xxx]]
}
astockwell commented 8 years ago

Update: This can be worked around via CiAscByField() (which does work after running just 1x) by defining a type/struct and coercing the child []string's to the struct.

patrickmn commented 8 years ago

wat

patrickmn commented 8 years ago

Something silly is going on here. Indeed it may have something to do with #2. Thanks for the report!

astockwell commented 8 years ago

Sure thing, let me know if I can lend a hand. Happy to write a PR if you have an inkling where the issue might lie.

patrickmn commented 8 years ago

Appreciate it.

I'm not sure yet, but I think the cause is the same as in #2.