www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sort(charArray) doesn't work

reply "Uplink_Coder" <someemail someprovider.some> writes:
sort doesn't work on an primitive Array ?
Is that normal ....
please look  http://www.dpaste.dzfl.pl/961286e1
because I don't know what i'm doing worng

Thanks in Advance :D
Nov 13 2013
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Uplink_Coder:

 sort doesn't work on an primitive Array ?
 Is that normal ....
 please look  http://www.dpaste.dzfl.pl/961286e1
 because I don't know what i'm doing worng

 Thanks in Advance :D
One way to solve your char[]: return cast(typeof(vars))vars.representation.sort().release; representation is in std.string. Bye, bearophile
Nov 13 2013
next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 13 November 2013 at 10:58:02 UTC, bearophile wrote:
 Uplink_Coder:

 sort doesn't work on an primitive Array ?
 Is that normal ....
 please look  http://www.dpaste.dzfl.pl/961286e1
 because I don't know what i'm doing worng

 Thanks in Advance :D
One way to solve your char[]: return cast(typeof(vars))vars.representation.sort().release; representation is in std.string. Bye, bearophile
How intuitive. :D
Nov 13 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, November 13, 2013 11:58:01 bearophile wrote:
 Uplink_Coder:
 sort doesn't work on an primitive Array ?
 Is that normal ....
 please look  http://www.dpaste.dzfl.pl/961286e1
 because I don't know what i'm doing worng
 
 Thanks in Advance :D
One way to solve your char[]: return cast(typeof(vars))vars.representation.sort().release;
And why not just return the original and avoid the cast? sort sorts in place. e.g. sort(vars.representation()); return vars; - Jonathan M Davis
Nov 13 2013
prev sibling next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
On Wednesday, 13 November 2013 at 10:32:19 UTC, Uplink_Coder 
wrote:
 sort doesn't work on an primitive Array ?
 Is that normal ....
 please look  http://www.dpaste.dzfl.pl/961286e1
 because I don't know what i'm doing worng

 Thanks in Advance :D
By the way: return sort(vars); //sort return a range i think, not a char[]; Maybe you want to do: return vars.sort; // (not sort in std.algorithms) or: import std.array; ... auto getAllVariables (Pair[][] parsedSysten) // Auto instead of char[] ... ... return sort(vars.array); // vars.array I'm not sure why sort(vars) doesn't work...
Nov 13 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Andrea Fontana:

 Maybe you want to do:

 return vars.sort;  // (not sort in std.algorithms)
That's deprecated (or going to be). Bye, bearophile
Nov 13 2013
prev sibling parent reply "Chris Cain" <clcain uncg.edu> writes:
On Wednesday, 13 November 2013 at 11:03:40 UTC, Andrea Fontana 
wrote:
 I'm not sure why sort(vars) doesn't work...
Basically, char[] is a (mutable) string. Since strings in D are treated as UTF, hasLength!(char[]) is actually false. Each "character" can actually be multiple bytes long, so it sort of makes sense. If you were to allow sorting of strings, then characters would be broken apart in the sorting process... so it doesn't do what you would expect it to do in those cases. So, the way around it is to just treat it as numbers (using std.string.representation, as bearophile pointed out) and sort it that way.
Nov 13 2013
parent "Uplink_Coder" <someemail someprovider.some> writes:
I don't want to have a string
but an array of letters maybe I justgo with ubyte
....
Solved it :D

return cast(char[])sort(cast(ubyte[])vars).release;
Nov 13 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, November 13, 2013 11:32:18 Uplink_Coder wrote:
 sort doesn't work on an primitive Array ?
 Is that normal ....
 please look  http://www.dpaste.dzfl.pl/961286e1
 because I don't know what i'm doing worng
 
 Thanks in Advance :D
Strings are treated as ranges of dchar, so if the element types is char or wchar, they're not random-access (since the number of code units per code point varies, meaning that each element in the array is a piece of a character and not necessarily an entire character), and sort requires random access. So, if you want to properly sort the string, you need to convert it to a random- access range - the mostly likely choice being dchar[], which you could do with to!(dchar[])(charArray), but of course, if you then want char[] again, you'd need to convert it back, e.g. to!(char[])(sort(to!(dchar[])(charArray))). However, if you're _certain_ that all of the characters are ASCII and therefore don't take more than one char, you can convert the string to ubyte[] and sort that. std.string.representation will do that for you. e.g. sort(representation(charArray)). But that _will_ mangle Unicode strings, so you need to be sure that your string only contains ASCII characters. - Jonathan M Davis
Nov 13 2013