| 1 |
// adapted from examples/array_sort.ck |
| 2 |
// sort the contents of arrays (various types) |
| 3 |
|
| 4 |
//----------------------------------------------------------------------------- |
| 5 |
// integer array |
| 6 |
//----------------------------------------------------------------------------- |
| 7 |
[ 5,2,-1,4,3,1,0,-2 ] @=> int arrayInt[]; |
| 8 |
// sort by ascending value |
| 9 |
arrayInt.sort(); |
| 10 |
// print |
| 11 |
for( auto x : arrayInt ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 12 |
// reverse for descending |
| 13 |
arrayInt.reverse(); |
| 14 |
// print |
| 15 |
for( auto x : arrayInt ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 16 |
|
| 17 |
|
| 18 |
//----------------------------------------------------------------------------- |
| 19 |
// float array |
| 20 |
//----------------------------------------------------------------------------- |
| 21 |
[ 2.0, -pi, 1, pi ] @=> float arrayFloat[]; |
| 22 |
// sort by ascending value |
| 23 |
arrayFloat.sort(); |
| 24 |
// print |
| 25 |
for( auto x : arrayFloat ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 26 |
|
| 27 |
|
| 28 |
//----------------------------------------------------------------------------- |
| 29 |
// complex array: sort by 2-norm/mag with phase as tie-breaker |
| 30 |
//----------------------------------------------------------------------------- |
| 31 |
[ #(-1,-1), #(1,1), #(0,0), #(1,0) ] @=> complex arrayComplex[]; |
| 32 |
// sort by ascending value |
| 33 |
arrayComplex.sort(); |
| 34 |
// print |
| 35 |
for( auto x : arrayComplex ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 36 |
|
| 37 |
|
| 38 |
//----------------------------------------------------------------------------- |
| 39 |
// polar array: sort by magnitude with phase as tie-breaker |
| 40 |
//----------------------------------------------------------------------------- |
| 41 |
[ %(2,pi/2), %(1,pi), %(2,pi) ] @=> polar arrayPolar[]; |
| 42 |
// sort by ascending value |
| 43 |
arrayPolar.sort(); |
| 44 |
// print |
| 45 |
for( auto x : arrayPolar ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 46 |
|
| 47 |
|
| 48 |
//----------------------------------------------------------------------------- |
| 49 |
// vec3 array: sort by magnitude with x then y then z as tie breakers |
| 50 |
//----------------------------------------------------------------------------- |
| 51 |
[ @(-1,-1,-1), @(1,2,1), @(0,0,0) ] @=> vec3 arrayVec3[]; |
| 52 |
// sort by ascending value |
| 53 |
arrayVec3.sort(); |
| 54 |
// print |
| 55 |
for( auto x : arrayVec3 ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 56 |
|
| 57 |
|
| 58 |
//----------------------------------------------------------------------------- |
| 59 |
// vec4 array: sort by magnitude with x then y then z then w as tie breakers |
| 60 |
//----------------------------------------------------------------------------- |
| 61 |
[ @(-1,-1,-1,-1), @(1,3,1,-2), @(0,0,0,0) ] @=> vec4 arrayVec4[]; |
| 62 |
// sort by ascending value |
| 63 |
arrayVec4.sort(); |
| 64 |
// print |
| 65 |
for( auto x : arrayVec4 ) { cherr <= x <= " "; } cherr <= IO.nl(); |
| 66 |
|
| 67 |
//----------------------------------------------------------------------------- |
| 68 |
// string array: sort alphabetically |
| 69 |
//----------------------------------------------------------------------------- |
| 70 |
[ "ge", "andrew", "nick", ] @=> string arrString[]; |
| 71 |
// sort by ascending value |
| 72 |
arrString.sort(); |
| 73 |
// print |
| 74 |
for( auto x : arrString ) { cherr <= x <= " "; } cherr <= IO.nl(); |