cannot sort an array of char

Ivan Kazmenko via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 6 02:52:31 PST 2014


On Wednesday, 5 November 2014 at 13:34:05 UTC, Marc Schütz wrote:
> On Wednesday, 5 November 2014 at 12:54:03 UTC, Ivan Kazmenko 
> wrote:
>> Hi!
>>
>> This gives an error (cannot deduce template function from 
>> argument types):
>>
>> -----
>> import std.algorithm;
>> void main () {
>> 	char [] c;
>> 	sort (c);
>> }
>> -----
>>
>> Why is "char []" so special that it can't be sorted?
>>
>> For example, if I know the array contains only ASCII 
>> characters, sorting it sounds no different to sorting an "int 
>> []".
>
> Hmm... this doesn't work either:
>
>     import std.algorithm;
>     import std.utf;
>     void main () {
>         char [] c;
>         sort (c.byCodeUnit);
>     }
>
> But IMO it should.

So, you imply that to use a char array as a RandomAccessRange, I 
have to use byCodeUnit? (and it should work, but doesn't?)

Fine, but how does one learn that except by asking here?  
Googling did not produce meaningful results for me.

For example, isRandomAccessRange[0] states the problem:
-----
Although char[] and wchar[] (as well as their qualified versions 
including string and wstring) are arrays, isRandomAccessRange 
yields false for them because they use variable-length encodings 
(UTF-8 and UTF-16 respectively). These types are bidirectional 
ranges only.
-----
but does not offer a solution.  If (when) byCodeUnit does really 
provide a random-access range, it would be desirable to have it 
linked where the problem is stated.

[0] http://dlang.org/phobos/std_range.html#.isRandomAccessRange


More information about the Digitalmars-d-learn mailing list