sort strings by interpreting them as integrals

Jonathan M Davis jmdavisProg at gmx.com
Sun Jul 29 19:56:08 PDT 2012


On Monday, July 30, 2012 04:48:02 bearophile wrote:
> Jonathan M Davis:
> > Otherwise, wouldn't _100 come before _20?
> 
> But he has said:
> > I need to treat these as if they were integrals, although the
> > underscore complicates things here since it should be ignored.
> > So the result I want is:
> > ["_10", "_20", "_100"]
> 
> No mention of 100 coming before 20.

I mean that wouldn't

assert("_100" < "_20");

pass? Which you obviously don't want. So, you need to use a predicate that 
takes that into account.

I just checked, and that comparison does indeed pass like I thought it would, 
so you can't just compare with <. You need a way to distinguish between 
numbers that start with smaller digits but are actually larger numbers, and 
length does that. The only problems that you get are if the number of hyphens 
can actually vary or if you could have leading zeroes - neither of which 
occurs in Andrej's example. And it avoids having to do any conversions like 
your solution does.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list