Liste des Groupes | Revenir à cl c |
Malcolm McLean <malcolm.arthur.mclean@gmail.com> 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.
Is an
implementation of qsort permitted to misbehave (for example by not
terminating) when the comparison function does not implement a proper
order relation?
Les messages affichés proviennent d'usenet.