ioutil.ReadDir sort order

1,643 views
Skip to first unread message

Arie van Wingerden

unread,
Aug 19, 2015, 5:40:41 AM8/19/15
to golang-nuts
The docs say: ReadDir reads the directory named by dirname and returns a list of sorted directory entries.
It does not say sorted by what. 
Maybe a doc update would be nice.
TIA

Mike Williamson

unread,
Aug 20, 2015, 8:37:48 AM8/20/15
to golang-nuts
By name, apparently:


Obviously trivial to resort them however you want, but yes, it would be better if the doc were clear.

shubham.p...@druva.com

unread,
Jul 6, 2019, 6:40:46 AM7/6/19
to golang-nuts
It sorts by name, but there is a big problem with golang string comparison.
If you consider these two strings:
str1 : "hello.20190305-102.txt"
str2 : "hello.20190305-99.txt"

Then we should say that str1 > str2.
But go returns str1 < str2

This message contains confidential information and is intended only for the individual to whom it is addressed. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and permanently delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, late or incomplete, or could contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required, please request a hard-copy version from the sender. Druva, www.druva.com

This message contains confidential information and is intended only for the individual to whom it is addressed. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and permanently delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, late or incomplete, or could contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required, please request a hard-copy version from the sender. Druva, www.druva.com

Dan Kortschak

unread,
Jul 6, 2019, 6:47:51 AM7/6/19
to shubham.p...@druva.com, golang-nuts
It's sorted lexically by the unicode code points. Why would str1 come
after str2? '1' < '9'.

On Fri, 2019-07-05 at 21:23 -0700, shubham.pendharkar via golang-nuts
wrote:
> It sorts by name, but there is a big problem with golang string
> comparison.
> If you consider these two strings:
> str1 : "hello.20190305-102.txt"
> str2 : "hello.20190305-99.txt"
>
> Then we should say that str1 > str2.
> But go returns str1 < str2
>
> On Wednesday, August 19, 2015 at 3:10:41 PM UTC+5:30, Arie van
> Wingerden
> wrote:
> >
> > The docs say: ReadDir reads the directory named by dirname and
> > returns a
> > list of sorted directory entries.
> > *It does not say sorted by what. *
> > Maybe a doc update would be nice.
> > TIA
> >
> >
>
> --
> This message contains confidential information and is intended only
> for the
> individual to whom it is addressed. If you are not the intended
> recipient,
> you should not disseminate, distribute or copy this e-mail. Please
> notify
> the sender immediately by e-mail if you have received this e-mail by
> mistake and permanently delete this e-mail from your system. E-mail
> transmission cannot be guaranteed to be secure or error-free as
> information
> could be intercepted, corrupted, lost, destroyed, late or incomplete,
> or
> could contain viruses. The sender therefore does not accept liability
> for
> any errors or omissions in the contents of this message, which arise
> as a
> result of e-mail transmission. If verification is required, please
> request
> a hard-copy version from the sender. Druva, www.druva.com
> <http://www.druva.com/>
>
> --
>
>
> This message contains confidential information and is intended only
> for
> the individual to whom it is addressed. If you are not the intended
> recipient, you should not disseminate, distribute or copy this e-
> mail.
> Please notify the sender immediately by e-mail if you have received
> this
> e-mail by mistake and permanently delete this e-mail from your
> system.
> E-mail transmission cannot be guaranteed to be secure or error-free
> as
> information could be intercepted, corrupted, lost, destroyed, late
> or
> incomplete, or could contain viruses. The sender therefore does not
> accept
> liability for any errors or omissions in the contents of this
> message,
> which arise as a result of e-mail transmission. If verification is
> required, please request a hard-copy version from the sender. Druva,
> www.druva.com <http://www.druva.com/>
>

Michael Jones

unread,
Jul 6, 2019, 10:06:36 AM7/6/19
to Dan Kortschak, golang-nuts, shubham.p...@druva.com
Indeed, you have two choices: create file names with fixed width numbers:

Printf “file%08dv%02d.dat”, f,v

Or do a string/number parse of the names before sorting and separate the numbers, you can insert spaces/zeroes and then string sort, or you can parse the numbers and compare them numerically. 

Sample code is in the Unix sort -n option and more specifically the multi key feature -k

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b5cb1e05924f6614eaca3eb280acd0aae44b631c.camel%40kortschak.io.
For more options, visit https://groups.google.com/d/optout.
--
Michael T. Jones
michae...@gmail.com
Reply all
Reply to author
Forward
0 new messages