Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide



Rockbox mail archive

Subject: Re: how is strnatcmp aka "Interpret numbers while sorting" supposed to sort?

Re: how is strnatcmp aka "Interpret numbers while sorting" supposed to sort?

From: Thomas Martitz <thomas.martitz_at_fhtw-berlin.de>
Date: Thu, 19 Mar 2009 16:20:47 +0100

Bryan VanDyke wrote:
> Thomas Martitz wrote:
>
>> Bryan VanDyke wrote:
>>
>>> Thomas Martitz wrote:
>>>
>>>
>>>> Linus Nielsen Feltzing wrote:
>>>>
>>>>
>>>>> Mike Holden wrote:
>>>>>
>>>>>
>>>>>> Maybe leading zeros should only be stripped if another digit follows
>>>>>> them?
>>>>>>
>>>>>> I use names like 00RockFaves.m3u, 00ClassicRock.m3u for playlists
>>>>>> that I
>>>>>> have created (as opposed to original artist albums), and the leading
>>>>>> zerozero is deliberately there to sort them at the top.
>>>>>>
>>>>>>
>>>>> That's an interesting observation. I believe leading zeroes are
>>>>> treated like whitespace in the current code, but in this case I think
>>>>> that the final zero should be kept.
>>>>>
>>>>> Linus
>>>>>
>>>>>
>>>> That's not trivial, and adds complexity. You basically need to look at
>>>> the current, the next, and one more for this, instead of just the
>>>> current char.
>>>>
>>>>
>>>>
>>> Actually it not that bad.
>>>
>>> Pseudo code:
>>>
>>> get current
>>> get next
>>> while (current != null && next != null && current == '0' && next is a
>>> number)
>>> {
>>> current = next
>>> next = get next
>>> }
>>>
>>>
>>>
>>>
>> Now imagine this for every char in a string, and for every string in a
>> file list (with some 100 files). It's three-times (or even more) more
>> complexity than just.
>> while (is_zero(a))
>> a = next;
>>
>> We're on embedded, and thus slow systems. Your would surely work well on
>> a desktop app, but for mp3-players we need fast and small code. The gain
>> has to justify the code, and I don't think it does it in this example.
>>
>>
>
> What about something like this. Taking in consideration the isspace
> function/comparison was removed? And isdigit is supposed to give nonzero
> on nodigit values.
>
> /* skip over leading zeros */
> while ('0' == ca && nat_isdigit(ca_next) )
> {
> ca = to_int(a[++ai]);
> ca_next = to_int(a[ai+1]);
> }
>
>
>
I've found a simpler solution for this. Trying the code raises the
following problem:

00 < 0b < 01 < 1

Zeros before except the final zeros are ignored, and the final zero
before characters is not ignored. But the leading zeros of numbers are
(so that 01 is 1). Obviously 0 sorts before 1.
Received on 2009-03-19

Page template was last modified "Tue Sep 7 00:00:02 2021" The Rockbox Crew -- Privacy Policy