www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array Sorting

reply 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
next sibling parent "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; // 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. :)
Jun 20 2007
prev sibling next sibling parent 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
prev sibling next sibling parent 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
prev sibling parent reply 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
parent reply 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
parent 0ffh <spam frankhirsch.net> writes:
Hoenir wrote:
 okibi wrote:
  > Putting a 0 in front is not an option.
I think I had a real bad day that day... :(
Jun 21 2007