Re: [go-nuts] Case Insensitive Sort

775 views
Skip to first unread message

andrey mirtchovski

unread,
Apr 30, 2013, 5:11:27 PM4/30/13
to Graham MacDonald, golang-nuts
you're looking for strings.EqualFold, most likely.

On Tue, Apr 30, 2013 at 2:45 PM, Graham MacDonald
<grahamam...@gmail.com> wrote:
> I have a string in a struct (os.FileInfo actually), and I want a case
> insensitive sort. I've implemented the sort interface for the using ToLower
> and the default string comparison as so:
>
> type ByPath []os.FileInfo
>
> func (s ByPath) Len() int { return len(s) }
> func (s ByPath) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
> func (s ByPath) Less(i, j int) bool {
> return strings.ToLower(s[i].Name()) < strings.ToLower(s[j].Name())
> }
>
> Is there a better way?
>
> Thanks,
> Graham
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Kyle Lemons

unread,
Apr 30, 2013, 7:04:54 PM4/30/13
to Graham MacDonald, golang-nuts
No, not yet.  What you're doing is not, strictly, the same thing as a case insensitive sort.  For ASCII, it has the same net result, but to do it correctly in general requires much more understanding of unicode and locales (see collation).  It's a nontrivial task that will probably take some time before a single solution emerges.

Kevin Gillette

unread,
Apr 30, 2013, 7:43:44 PM4/30/13
to golan...@googlegroups.com, Graham MacDonald
EqualFold tests for equality, not less-ness.

andrey mirtchovski

unread,
Apr 30, 2013, 7:50:46 PM4/30/13
to Kevin Gillette, golang-nuts, Graham MacDonald
my bad. seems like i didn't read the requirements at all :)

Hotei

unread,
Apr 30, 2013, 10:39:36 PM4/30/13
to golan...@googlegroups.com
An option to consider: go get github.com/pmylund/sortutil - has CI sort and other goodies (N.B. author is Patrick Mylund Neilsen, I'm just a happy user of said package)

Graham MacDonald

unread,
May 1, 2013, 1:51:12 AM5/1/13
to golan...@googlegroups.com, Graham MacDonald
Thanks for the answer.  I appreciate it's hard, and TBH, doubted it existed (I did look!)

Given that the full solution is hard, it seems like it would be worth implementing  something that would satisfy most cases, if that is much easier (assuming it's properly tagged as an AsciiStringComparer (or whatever)).  The complete solution could be done later.

Graham MacDonald

unread,
May 1, 2013, 1:53:30 AM5/1/13
to golan...@googlegroups.com
Thanks - that looks useful.

Gerard

unread,
May 1, 2013, 4:29:13 AM5/1/13
to golan...@googlegroups.com, Graham MacDonald
I have this silly idea about adding extra lower case string fields to the type just before sorting. Then the ToLower function only takes place once (for each member of the array). After the sorting you can remove these strings. It's simple.

Op woensdag 1 mei 2013 07:51:12 UTC+2 schreef Graham MacDonald het volgende:

Gerard

unread,
May 1, 2013, 5:33:26 AM5/1/13
to golan...@googlegroups.com, Graham MacDonald
It looks like this
 
type ByPath []byPath

type byPath struct {
os.FileInfo
sl string
}

func (s ByPath) PrepareForSort() { 
for i := range s {
s[i].sl = strings.ToLower(s[i].Name())
}
}

func (s ByPath) Len() int      { return len(s) }
func (s ByPath) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByPath) Less(i, j int) bool { return s[i].sl < s[j].sl }


Op woensdag 1 mei 2013 10:29:13 UTC+2 schreef Gerard het volgende:
Reply all
Reply to author
Forward
0 new messages