Thread overview | |||||
---|---|---|---|---|---|
|
July 30, 2012 sort strings by interpreting them as integrals | ||||
---|---|---|---|---|
| ||||
void main() { string[] x = ["_100", "_10", "_20"]; sort(x); writeln(x); } result: ["_10", "_100", "_20"] I need to treat these as if they were integrals, although the underscore complicates things here since it should be ignored. So the result I want is: ["_10", "_20", "_100"] How would you implement that? It could be a nice exercise! :) |
July 30, 2012 Re: sort strings by interpreting them as integrals | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> void main()
> {
> string[] x = ["_100", "_10", "_20"];
> sort(x);
> writeln(x);
> }
>
> result:
> ["_10", "_100", "_20"]
>
> I need to treat these as if they were integrals, although the
> underscore complicates things here since it should be ignored. So the
> result I want is:
> ["_10", "_20", "_100"]
import std.stdio, std.algorithm, std.conv;
void main() {
auto data = ["_100", "_10", "_20"];
schwartzSort!(s => to!int(s[1 .. $]))(data);
writeln(data);
}
Bye,
bearophile
|
July 30, 2012 Re: sort strings by interpreting them as integrals | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 7/30/12, bearophile <bearophileHUGS@lycos.com> wrote:
> import std.stdio, std.algorithm, std.conv;
> void main() {
> auto data = ["_100", "_10", "_20"];
> schwartzSort!(s => to!int(s[1 .. $]))(data);
> writeln(data);
> }
Very nice! Thanks.
|
Copyright © 1999-2021 by the D Language Foundation