digitalmars.D.learn - sort(charArray) doesn't work
- Uplink_Coder (5/5) Nov 13 2013 sort doesn't work on an primitive Array ?
- bearophile (6/11) Nov 13 2013 One way to solve your char[]:
- Namespace (2/14) Nov 13 2013 How intuitive. :D
- Jonathan M Davis (6/17) Nov 13 2013 And why not just return the original and avoid the cast? sort sorts in p...
- Andrea Fontana (15/20) Nov 13 2013 By the way:
- bearophile (4/6) Nov 13 2013 That's deprecated (or going to be).
- Chris Cain (11/12) Nov 13 2013 Basically, char[] is a (mutable) string. Since strings in D are
- Uplink_Coder (5/5) Nov 13 2013 I don't want to have a string
- Jonathan M Davis (15/21) Nov 13 2013 Strings are treated as ranges of dchar, so if the element types is char ...
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
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 :DOne way to solve your char[]: return cast(typeof(vars))vars.representation.sort().release; representation is in std.string. Bye, bearophile
Nov 13 2013
On Wednesday, 13 November 2013 at 10:58:02 UTC, bearophile wrote:Uplink_Coder:How intuitive. :Dsort 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 :DOne way to solve your char[]: return cast(typeof(vars))vars.representation.sort().release; representation is in std.string. Bye, bearophile
Nov 13 2013
On Wednesday, November 13, 2013 11:58:01 bearophile wrote:Uplink_Coder:And why not just return the original and avoid the cast? sort sorts in place. e.g. sort(vars.representation()); return vars; - Jonathan M Davissort 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 :DOne way to solve your char[]: return cast(typeof(vars))vars.representation.sort().release;
Nov 13 2013
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 :DBy 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
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
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
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
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 :DStrings 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