Sorting an Array
okibi
okibi at ratedo.com
Tue Jul 10 11:34:26 PDT 2007
Well, the following comment made it seem like it won't work:
>> // comparing sorting algorithm goes here. See wikipedia for examples.
>> // Quicksort should do nicely and is easy to implement.
Regan Heath Wrote:
> I believe the array.sort routine uses Quicksort. So, the code below
> already implements it.
>
> Regan
>
> okibi wrote:
> > This does seem like the best way, but I'm not sure how I would go about implement Quicksort into that code. Could you please give me some pointers?
> >
> > Thanks!
> >
> > 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