[Gambas-user] Natural sorting in Gambas 3

Benoît Minisini gambas at ...1...
Mon Jan 25 20:16:30 CET 2010


Hi,

I have implemented string natural comparison and sorting in the last Gambas 3 
revision.

With that, you can, for example, sort a file name array returned by Dir() that 
way:

	Photo1.jpg Photo1a.jpg Photo1b.jpg Photo2.jpg ... Photo9.jpg Photo10.jpg
	Photo11.jpg

Instead of:

	Photo1.jpg Photo10.jpg Photo 11.jpg Photo1a.jpg Photo1b.jpg Photo2.jpg ... 
	Photo9.jpg

For that, use the gb.Natural constant as sort flag.

Moreover, if you have non-ASCII letters, you can add gb.Language to the flag, 
and non-ASCII characters will be correctly sorted too. You will get:

	Baba.jpg Bébé1.jpg Bébé8.jpg Bébé20.jpg Bibi.jpg Bobo.jpg

instead of:

	Baba.jpg Bibi.jpg Bobo.jpg Bébé1.jpg Bébé20.jpg Bébé8.jpg 

How does it work?

The string is splitted between parts having digits only (0-9), and parts 
having no digits.

Parts having no digits are sorted as normal strings. 

Parts having digits are sorted according to their numerical values, unless it 
starts with a zero.

Enjoy it!

-- 
Benoît Minisini




More information about the User mailing list