Sorting an Array

okibi okibi at ratedo.com
Tue Jul 10 11:55:19 PDT 2007


I couldn't get your function to compile. I get the following error:

Error: need 'this' to access member getNumericPart

On the line:

return getNumericPart(a)>getNumericPart(b);

Any ideas?

downs Wrote:

> okibi wrote:
> > Is there an easy way to sort an array with elements such as "12 - Some text" or "241 - Other Text"?
> > 
> > I want to sort in order of the numbers. Is this possible?
> > 
> > Thanks!
> // Sort array via comparison operator.
> // 'bigger' returns if 'value' is bigger than 'than'.
> void sort(T)(ref T[] array, bool function(T value, T than) bigger) {
>    // comparing sorting algorithm goes here. See wikipedia for examples.
>    // Quicksort should do nicely and is easy to implement.
> }
> 
> long getNumericPart(char[] inp) {
>    if (!input.length) return long.max+1; // no number is very small.
>    size_t numlen=0;
>    while (numlen<inp.length)&&(inp[numlen]>='0')&&(inp[numlen]<='9'))
>      ++numlen;
>    return inp[0..numlen];
> }
> 
> void main() {
>    char[][] array=getArray(); // array is read
>    array.sort(function(char[] a, char[] b) {
>      return getNumericPart(a)>getNumericPart(b);
>    });
> }
> 
> Didn't try, obviously, but it should work.
> Have fun! :D
>   --downs



More information about the Digitalmars-d-learn mailing list