Sorting an Array

downs default_357-line at
Tue Jul 10 11:37:53 PDT 2007

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'))
   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

More information about the Digitalmars-d-learn mailing list