io.ioutil.ReadDir sort by default

1,786 views
Skip to first unread message

GGGO

unread,
Feb 9, 2012, 6:58:34 PM2/9/12
to golang-nuts
What do you think about removing the line sort.Sort(byName(list)) in
io.ioutil.ReadDir() and let the user choose his sort(by decrease, by
increase, by any field), if he need to.
This avoids re-sorting multiple time. Maybe it could result in gain
performance in some situation...I don't know.
In my case, I need to sort by ModTime so I do :

type byDate []os.FileInfo
func (f byDate) Len() int {
return len(f)
}
func (f byDate) Less(i, j int) bool {
return f[i].ModTime().Unix() > f[j].ModTime().Unix()
}
func (f byDate) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}

// main
t, err := ioutil.ReadDir(".")
if err != nil {
}
sort.Sort(byDate(t)) // here I sort a sorted slice

What do you think ?

Thanks.

peterGo

unread,
Feb 9, 2012, 9:15:47 PM2/9/12
to golang-nuts
GGGO,

code review 3458041: io/ioutil: configurable sort mode in ReadDir
(issue3458041)
http://groups.google.com/group/golang-dev/browse_thread/thread/8aeee1502ea90c5d

code review 3458041: io/ioutil: give ReadDir a sorting function
(issue3458041)
http://groups.google.com/group/golang-dev/browse_thread/thread/c4ff7d29013dba9d

Peter

GGGO

unread,
Feb 9, 2012, 10:01:44 PM2/9/12
to golang-nuts
So, why not simply remove one line and let the user decide.
I'm agree to not add more arguments to ReadDir.

In my opinion, ReadDir as his name say, list the directory, not more.
If you don't need to show the result to the screen and just want to
make some other stuff with the list, it doesn't matter to have a
sorted slice.

sort.Interface is so powerful and easy for that.

On Feb 9, 9:15 pm, peterGo <go.peter...@gmail.com> wrote:
> GGGO,
>
> code review 3458041: io/ioutil: configurable sort mode in ReadDir
> (issue3458041)http://groups.google.com/group/golang-dev/browse_thread/thread/8aeee1...
>
> code review 3458041: io/ioutil: give ReadDir a sorting function
> (issue3458041)http://groups.google.com/group/golang-dev/browse_thread/thread/c4ff7d...

Rémy Oudompheng

unread,
Feb 10, 2012, 2:22:40 AM2/10/12
to GGGO, golang-nuts
On 2012/2/10 GGGO <ggco...@gmail.com>:

> What do you think about removing the line sort.Sort(byName(list)) in
> io.ioutil.ReadDir() and let the user choose his sort(by decrease, by
> increase, by any field), if he need to.
> This avoids re-sorting multiple time. Maybe it could result in gain
> performance in some situation...I don't know.
> In my case, I need to sort by ModTime so I do :

You can copy the ReadDir source code and customize it. Anyway, I think
the os.(*File).ReadDir call is hugely slower than the Sort call and
your way of doing doesn't look so bad.

Rémy.

Russ Cox

unread,
Feb 10, 2012, 11:51:43 AM2/10/12
to GGGO, golang-nuts
On Thu, Feb 9, 2012 at 18:58, GGGO <ggco...@gmail.com> wrote:
> What do you think about removing the line sort.Sort(byName(list)) in
> io.ioutil.ReadDir() and let the user choose his sort(by decrease, by
> increase, by any field), if he need to.

It's a utility. It's not the only way to read a directory.
If it isn't useful in your program, don't use it.

Russ

GGGO

unread,
Feb 10, 2012, 11:54:24 AM2/10/12
to golang-nuts
Yes, I understood this yesterday, and I just copied code to mine.
Thnaks.

On Feb 10, 11:51 am, Russ Cox <r...@golang.org> wrote:
Reply all
Reply to author
Forward
0 new messages