Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 20, 2007 Array Sorting | ||||
---|---|---|---|---|
| ||||
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! |
June 20, 2007 Re: Array Sorting | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | "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; // tada 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. :) |
June 20, 2007 Re: Array Sorting | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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!
|
June 20, 2007 Re: Array Sorting | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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;
}
|
June 20, 2007 Re: Array Sorting | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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
|
June 21, 2007 Re: Array Sorting | ||||
---|---|---|---|---|
| ||||
Posted in reply to 0ffh | 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.
|
Copyright © 1999-2021 by the D Language Foundation