1 
2 /*
3  * Placed into Public Domain
4  * written by Walter Bright
5  * www.digitalmars.com
6  *
7  * This is a public domain version of qsort.d.
8  * All it does is call C's qsort(), but runs a little slower since
9  * it needs to synchronize a global variable.
10  */
11 
12 /*
13  *  Modified by Sean Kelly <sean@f4.ca> for use with Tango.
14  */
15 module rt.qsort2;
16 
17 //debug=qsort;
18 
19 private import tango.stdc.stdlib : qsort;
20 
21 private TypeInfo tiglobal;
22 
23 extern (C) int cmp(void* p1, void* p2)
24 {
25     return tiglobal.compare(p1, p2);
26 }
27 
28 extern (C) void[] _adSort(void[] a, TypeInfo ti)
29 {
30     synchronized
31     {
32         tiglobal = ti;
33         qsort(a.ptr, a.length, cast(size_t)ti.tsize(), &cmp);
34     }
35     return a;
36 }
37 
38 
39 
40 unittest
41 {
42     debug(qsort) printf("array.sort.unittest()\n");
43 
44     int a[] = new int[10];
45 
46     a[0] = 23;
47     a[1] = 1;
48     a[2] = 64;
49     a[3] = 5;
50     a[4] = 6;
51     a[5] = 5;
52     a[6] = 17;
53     a[7] = 3;
54     a[8] = 0;
55     a[9] = -1;
56 
57     a.sort;
58 
59     for (int i = 0; i < a.length - 1; i++)
60     {
61         //printf("i = %d", i);
62         //printf(" %d %d\n", a[i], a[i + 1]);
63         assert(a[i] <= a[i + 1]);
64     }
65 }