Sort list of paths


(Fookiwara) #1

Hello,
I would like to sort the following list:
paths := []string{a/b/c,a, b/x, a (2), bat, a/b, b}
I would like to obtain this result:
a
a/b
a/b/c
a (2)
b
b/x
bat

Is this code Ok or can we sort it faster?
https://play.golang.org/p/MVCiyf1BDw8

Any idea?
Regards


(Johan Dahl) #2

It looks good. The only complaint I have is that Idx should not have a capital I because this is reserved for exported function, types and vars.

You could do one thing which could speed it up. First split all paths and get a slice of slices of string and then do the sort and then concatenate them again. In this way would not strings.Split be called for every Less and it would go faster especially if slice is long.

One other way is to replace / with a character with a very low value and compare the strings after that. It would work. Like https://play.golang.org/p/XSW0kGILJbF


(Fookiwara) #3

The replace hack is a the solution I was looking for. Thanks!