Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Baby X is bor nagain
Date: Thu, 20 Jun 2024 08:05:24 -0700
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <86y16zhcy3.fsf@linuxsc.com>
References: <20240613002933.000075c5@yahoo.com> <20240613174354.00005498@yahoo.com> <20240617002924.597@kylheku.com> <20240618115650.00006e3f@yahoo.com> <20240618184026.000046e1@yahoo.com> <877celzx14.fsf@nosuchdomain.example.com> <87iky3svqh.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Thu, 20 Jun 2024 17:05:25 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="8ac6802ee4910c68ec471994eb4b3109"; logging-data="2755781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IA+hqcC5Kb9ObIFad7mO1q3BvwYz19Sw="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:51ldPCoYKd52FDRBXdw5TcMhzFM= sha1:NyVwyxZIVzZpB88ZYkpyGfWL/Aw=
Xref: csiph.com comp.lang.c:386271
Ben Bacarisse writes:
> Malcolm McLean writes:
>
>> On 20/06/2024 08:55, David Brown wrote:
>
> ...
>
>>> You've made it clear you know nothing about the language. Fair enough -
>>> we all know almost nothing about almost all programming languages. But
>>> trust someone who does.
>>
>> Yes but I just don't.
>> Everyone says "oh yes, that is easy - goto a Python group" and so
>> on. No-one actually comes up with the code.
>>
>> In C, what we do is write a special version of strcmp
>>
>> int strcmp_light_and_dark(const char *a, const char *b)
>> {
>> int i;
>>
>> for (i = 0; a[i] && b[i]; i++)
>> {
>> if (a[i] != b[i])
>> {
>> if (a[i] == 'L' && b[i] == 'D' && i > 0 && a[i-1] == '_')
>> return -1;
>> if (a[i] == 'D' && b[i] == 'L' && i > 0 && a[i-1] = '_')
>> return 1;
>> break;
>> }
>> }
>>
>> return a[i] - b[i];
>>
>> }
>>
>> So easy to do.
>
> Unless I'm missing something here, that code does not do what you say
> you want. You gave an example of some input and the desired output but
> this comparison function does not sort into the ordering you gave.
>
> You may find this "ordering" hard to duplicate in other languages
> because it is not even an ordering in the mathematical sense as it is
> not transitive.
>
> Can you specify the desired ordering as a total or partial order
> relation?
>
> On a C language point, I don't think the standard says anything about
> sorting with non-order functions like the one above.
It does, somewhat indirectly, in section 7.22.5 paragraph 4.
> Is an
> implementation of qsort permitted to misbehave (for example by not
> terminating) when the comparison function does not implement a proper
> order relation?
My reading of the C standard is that the comparison function
must impose a total ordering on the elements actually present
in the array, or is undefined behavior if it does not. In
other words it's okay if the comparison function doesn't
define a proper order relation, as long as there are no
inconsistencies between values that actually occur in the
particular array being sorted.