[Issue] Dir: random sorting with Linux

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Bidouille
Volunteer
Posts: 574
Joined: Mon Nov 19, 2007 10:58 am
Location: France

[Issue] Dir: random sorting with Linux

Post by Bidouille »

Hello,
A folder contains 5 files:
  • chat001.jpg
  • chat002.jpg
  • chat398.jpg
  • chat401.jpg
  • chat690.jpg
If I execute this code:

Code: Select all

Sub Main
	path = "~/test/"
	myFile = Dir(path & "*")
	While myFile <> ""
		url = ConvertToUrl(path & myFile)
		result = result + url + chr(10)
		myFile = Dir
	Wend
	msgbox result
End Sub
The result is sorted non alphabetically.
Image
Tested with AOO 4.1.1 and Debian 7 x64

Could you confirm the bug?
Last edited by Bidouille on Thu Mar 19, 2015 11:30 am, edited 1 time in total.
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Dir: random sorting with Linux

Post by Villeroy »

This is not a bug. The order of files queried from the file system is up to the file system. If you want a sorted list then sort it.
Same with this code:

Code: Select all

srv = createUnoService("com.sun.star.ucb.SimpleFileAccess")
dirlist = srv.getFolderContents("file:///home/user/test", True)
msgbox join(dirlist, Chr(10))
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Bidouille
Volunteer
Posts: 574
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Dir: random sorting with Linux

Post by Bidouille »

Villeroy wrote:The order of files queried from the file system is up to the file system.
Despite several attempts in a console with ls command, I have not found in what order was sorted files.
_dir_linux.gif
Villeroy wrote:If you want a sorted list then sort it.
With Windows, Dir returns files list correctly.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Dir: random sorting with Linux

Post by RoryOF »

@Bidouille: I think you need to look at the file save time seconds to get the exact order of the sorted files. In your screenshot Chat398.jpg, chat401.jpg and chat002.jpg are all shown with date/time of 16/03/2015 15:58, but they may all differ in the seconds place, which you do not display but which a time based sort will take into account.

As Villeroy points out, it is logical that your subroutine returns the files in the order the operating system filehandler sees them. Unless there are API parameters to cause a directory query to be returned in sorted order (I don't know - I haven't looked), your best approach may be to read the directory names into an array and call a sort routine.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Dir: random sorting with Linux

Post by acknak »

try ls -U (unsorted)

It's possible that the names are returned in the order that they appear in the directory on disk, which, as Villeroy pointed out, could be anything.
AOO4/LO5 • Linux • Fedora 23
Bidouille
Volunteer
Posts: 574
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Dir: random sorting with Linux

Post by Bidouille »

OK, I resume:
Basic Windows = Dir
Basic Linux = ls -U

So, it would be better that:
Basic Linux = ls

RFE reported: https://bz.apache.org/ooo/show_bug.cgi?id=126186
Post Reply