std.array suggestion

Oskar Linde olREM at OVEnada.kth.se
Fri Mar 10 23:30:58 PST 2006


Kevin Bealer wrote:

> In article <dus0hn$2tb0$1 at digitaldaemon.com>, Oskar Linde says...

>>Consider sorting phone-book entries (Sorted by first city, then surname
>>and finally first name):
>>
>>bool phoneBookOrder(Record a, Record b) {
>>return a.city > b.city ||
>>                a.surname > b.surname ||
>>                a.name > b.name;
>>}
>>

> This contains a bug.  It should be like this:
> 
>>bool phoneBookOrder(Record a, Record b) {
>>    if (a.city < b.city) return false;
>>    if (a.city > b.city) return true;
>>
>>    if (a.surname < b.surname) return false;
>>    if (a.surname > b.surname) return true;
>>
>>    return a.name > b.name;
>>}
> 
> .. Otherwise, (a < b) && (b < a) is possible, i.e. when the cities are
> equal.

You are correct. How embarassing. I was sure I had read the above example
somewhere, so I didn't care to think before typing it in...

/Oskar



More information about the Digitalmars-d mailing list