Issue 126186 - Linux: Dir function should sorted in alphabetical order
Summary: Linux: Dir function should sorted in alphabetical order
Status: UNCONFIRMED
Alias: None
Product: App Dev
Classification: Unclassified
Component: api (show other issues)
Version: 4.1.1
Hardware: PC Linux, all
: P5 (lowest) Normal
Target Milestone: ---
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords: needmoreinfo
Depends on:
Blocks:
 
Reported: 2015-03-17 15:09 UTC by oooforum (fr)
Modified: 2015-03-19 15:43 UTC (History)
2 users (show)

See Also:
Issue Type: ENHANCEMENT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description oooforum (fr) 2015-03-17 15:09:54 UTC
Under Windows, Dir function returns a list of files in alphabetical order. Same result than "Dir" command in a console.
With Linux, this function returns a list of files in creating order. Same result than "ls -f" command.
So, why not have an equivalent of "ls" command by default and generate a list of files in alphabetical order?

Source code available in:
svn.apache.org/repos/asf/openoffice/trunk/main/basic/source/runtime/methods.cxx
Maybe an easy hack but I don't have any knowledege in C++
Comment 1 orcmid 2015-03-17 20:09:55 UTC
What Apache OpenOffice function are you talking about?  That is, where do you see a directory listing from AOO that is not in the order you want?

In Windows, the Dir function will sort in many different ways.  The default happens to be alphabetical order on file name.  That is true in the Windows File Explorer too.  And ls.

Often when a directory is accessed through OS APIs, the files return in directory order without sorting.  

Can you narrow this down please in terms of where this is seen?
Comment 2 oooforum (fr) 2015-03-18 10:44:03 UTC
(In reply to orcmid from comment #1)
> What Apache OpenOffice function are you talking about?  
Sorry for lack of informations.
I'm talking about Basic and its Dir internal function:
https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Files_and_Directories_%28Runtime_Library%29

See: https://forum.openoffice.org/en/forum/viewtopic.php?t=76011
More detailed what I mean
Comment 3 orcmid 2015-03-18 15:40:39 UTC
(In reply to oooforum from comment #2)
> (In reply to orcmid from comment #1)
> > What Apache OpenOffice function are you talking about?  
> Sorry for lack of informations.
> I'm talking about Basic and its Dir internal function:
> https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/
> Files_and_Directories_%28Runtime_Library%29
> 
> See: https://forum.openoffice.org/en/forum/viewtopic.php?t=76011
> More detailed what I mean

Thank you.  Here's what I think the difficulty is.

The TL;DR: The list provided by the Basic DIR function retrieves file and directory entries in exactly the sequence they are returned by the interface provided by the operating system.  It is meant to be only that, with providing higher-level functionality left for user-defined functions that use DIR internally.  It is not a defect.  That is by design.

I am changing this incident report to be for an ENHANCEMENT, not a DEFECT, that senior developers can confirm.  I expect this will be closed as not an issue after there is confirmation.

WORKAROUND

To provide what you want, the usual approach is to define and use a function that scans a directory, employs the wild card filtering, and delivers the result in an Object having a list of directory-information strings that are sorted in some particular manner.

The DIR operation is intentionally much lower-level than that, but it provides basic operations that higher-level user-defined functions can use.

There might be such solutions already on the Community Forum or other web sites where Basic tips are found.  


DETAILED BACKGROUND

The difference between Basic DIR and command-line utilities such as Unix ls and Microsoft (DOS) DIR, is that *the*utility* sorts the list before presenting it.  Those allow you to control the direction of sequencing, and choose which field(s) sequencing is by.  That is all in the utility, which provides this above the operating-system API.  This is efficient for the utility because it is designed to retrieves a complete list and then sort it once.  You can also see the delay of sorting if you access a large directory (especially if you go recursively into subdirectories).

As explained in the BASIC Guide, mapping DIR to the operating systems lows-level directory access is intentional and is meant to be limited to a common approach available across all platforms.  This means that it delivers whatever the operating system delivers, not what is produced by shell utilities built atop the operating system.  The operating system keeps track of where the DIR command is and what the next position is, but it is a position in whatever sequence that directory happens to be in.  (Some systems have utilities that sort the directory *in* the file system, in which case a DIR would happen to deliver files in that order.)

At the level of the Basic DIR, there is no determination that the BASIC program will retrieve all of the entries or is even interested in all of them.  The simple wild-card filtering is usually also provided by the operating system. Any sorting should be done after that filtering, not before, of course.  DIR is too simple to do that part.
Comment 4 orcmid 2015-03-18 16:12:39 UTC
(In reply to oooforum from comment #0)
> Source code available in:
> svn.apache.org/repos/asf/openoffice/trunk/main/basic/source/runtime/methods.
> cxx
> Maybe an easy hack but I don't have any knowledege in C++

Thanks for the link to the source-code repository.  I took a quick look at RTLFUNC(dir) and confirmed that (1) there is no easy hack and (2) this is not the level of abstraction at which DIR is defined for OpenOffice Basic.

If you are seeing sorting in retrievals from directories when running AOO on Windows, I did not notice anywhere that is being provided by OpenOffice Basic.

Also, <http://stackoverflow.com/questions/4282940/does-dir-make-any-guarantee-on-the-order-of-files-returned> is informative but that may be a function of what DIR is being applied to.
Comment 5 orcmid 2015-03-18 16:21:14 UTC
(In reply to orcmid from comment #3)
> [...]  The operating system keeps track
> of where the DIR command is and what the next position is, but it is a
> position in whatever sequence that directory happens to be in.  

Many typos in that long note.  (Bugzilla doesn't provide editing of existing comments, but the typos are pretty obvious.)

One clarification.  The RTLFUNC(dir) may be holding onto a placeholder that the OS needs to be given to obtain the next entry when the DIR operations for subsequent retrievals are requested.  But there is no sorting.  Just enough to continue whatever sequence the OS has.
Comment 6 orcmid 2015-03-18 16:24:14 UTC
(In reply to orcmid from comment #3)
> [...]
> The DIR operation is intentionally much lower-level than that, but it
> provides basic operations that higher-level user-defined functions can use.
> 
> There might be such solutions already on the Community Forum or other web
> sites where Basic tips are found.  

The surprising appearance of out-of-order DIR results is apparently a common issue.

Here is an older solution to this, <http://www.vb-helper.com/howto_sorted_dir.html>.
Comment 7 oooforum (fr) 2015-03-19 09:28:44 UTC
(In reply to orcmid from comment #4)
> there is no easy hack 
Thanks for your investigations.

>The list provided by the Basic DIR function retrieves file and directory
> entries in exactly the sequence they are returned by the interface
> provided by the operating system.
We should complete helpfile with this information.
Pootle Apache OpenOffice 4.x Help:  helpcontent2/source/text/sbasic/shared.po
Comment 8 orcmid 2015-03-19 15:43:28 UTC
(In reply to oooforum from comment #7)
> (In reply to orcmid from comment #4)
> >The list provided by the Basic DIR function retrieves file and directory
> > entries in exactly the sequence they are returned by the interface
> > provided by the operating system.
> We should complete helpfile with this information.
> Pootle Apache OpenOffice 4.x Help:  helpcontent2/source/text/sbasic/shared.po

Agreed