## digitalmars.D.learn - Array Sorting

• okibi (14/14) Jun 20 2007 Hey,
• Jarrett Billingsley (25/40) Jun 20 2007 I take it you have an array of strings.
• Tristam MacDonald (3/24) Jun 20 2007 Is this an array of integers, or an array of strings containing integer ...
• Jari-Matti =?ISO-8859-1?Q?M=E4kel=E4?= (17/39) Jun 20 2007 Slightly modified version from the Tango manual:
• 0ffh (4/5) Jun 20 2007 If you absolutely /must/ sort the numbers a strings instead of
• Hoenir (2/5) Jun 21 2007
• 0ffh (2/4) Jun 21 2007 I think I had a real bad day that day... :(
okibi <okibi ratedo.com> writes:
```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!
```
Jun 20 2007
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
```"okibi" <okibi ratedo.com> wrote in message
news:f5b5a1\$187u\$1 digitalmars.com...
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!

I take it you have an array of strings.

Unfortunately the array .sort property does not allow you to use a custom
sorting predicate, and phobos doesn't provide any predicate-using sort
function like Tango does.  You can get by pretty well by using a struct
array:

struct NumString
{
char[] data;

int opCmp(NumString* other)
{
int myNum = toInt(data);
int otherNum = toInt(other.data);

return myNum - otherNum;
}
}

...

NumString[] arr = new NumString[10];
// fill it
arr.sort;

Or, you can write a function that will take an array and a sorting
predicate, and do the sort yourself.  Or, you can use the C stdlib qsort
(but that's not pretty at all).  Or use Tango.  Or use Cashew.  :)
```
Jun 20 2007
Tristam MacDonald <swiftcoder gmail.com> writes:
```Is this an array of integers, or an array of strings containing integer values?
I take it it is the latter, since the sort is being performed
lexicographically, rather than numerically. In that case, probably the easiest
would be to convert your strings to integers to sort them, and convert back to
strings afterwards.

You could also, of course, write a custom sort function.

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!

```
Jun 20 2007
Jari-Matti =?ISO-8859-1?Q?M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
```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;
}
```
Jun 20 2007
0ffh <spam frankhirsch.net> writes:
```okibi wrote:
I was wondering if there is a way to sort three digit numbers.

If you absolutely /must/ sort the numbers a strings instead of
as numbers, try to prepend zeros to bring the to equal length.

Regards, frank
```
Jun 20 2007
Hoenir <mrmocool gmx.de> writes:
```0ffh wrote:
If you absolutely /must/ sort the numbers a strings instead of
as numbers, try to prepend zeros to bring the to equal length.

okibi wrote:
Putting a 0 in front is not an option.

```
Jun 21 2007
0ffh <spam frankhirsch.net> writes:
```Hoenir wrote:
okibi wrote:
> Putting a 0 in front is not an option.