Array Sorting

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Wed Jun 20 05:53:33 PDT 2007


okibi wrote:

> Hey,
> 
> I was wondering if there is a way to sort three digit numbers.
> 
> Doing an array.sort will result in a sequence such as this:
> 
> 1
> 10
> 2
> 24
> 
> What I want is it to get sorted like this:
> 
> 1
> 2
> 10
> 24
> 
> Is there an easy way to specify this? Putting a 0 in front is not an
> option.
> 
> Thanks!

Slightly modified version from the Tango manual:

import tango.core.Array;
import tango.text.Ascii;
import tango.io.Console;

void main()
{
    char[][] n;

    n ~= "1";
    n ~= "10";
    n ~= "2";
    n ~= "24";

    n.sort( ( char[] l, char[] r ) { return (l.length - r.length) ? l.length
< r.length : icompare(r,l); } );

    foreach( num; n )
        Cout (num).newline;
}



More information about the Digitalmars-d-learn mailing list