sortutil icon indicating copy to clipboard operation
sortutil copied to clipboard

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

Open astockwell opened this issue 10 years ago • 5 comments

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 avatar Jan 12 '16 20:01 astockwell

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.

astockwell avatar Jan 12 '16 20:01 astockwell

wat

patrickmn avatar Jan 12 '16 20:01 patrickmn

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

patrickmn avatar Jan 12 '16 20:01 patrickmn

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.

astockwell avatar Jan 12 '16 21:01 astockwell

Appreciate it.

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

patrickmn avatar Jan 12 '16 21:01 patrickmn