1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| public static function quickSort(list:Array):Array
{
// choose first element as pivot
var pivot:String = list[0].keyword
// slice of the pivot and keep them as rest
var rest:Array = list.slice(1)
return(
// sort all numbers…
quickSort
(
// …that are less or equal than pivot…
rest.filter
(
wrap
(
greaterThan(pivot)
)
)
)
// …concatenate them with the pivot…
.concat
(
[pivot]
)
// …and concatenate all of the previous…
.concat
(
// …with all sorted numbers…
quickSort
(
// …that are greater than pivot.
rest.filter
(
wrap
(
lessOrEqualThan( pivot )
)
)
)
)
)
/// return rest;
}
public static function lessOrEqualThan(value:String):Function
{
return(
function( x:String ) : Boolean
{
return x.length <= value.length
}
)
}
public static function greaterThan(value:String):Function
{
return(
function( x:String):Boolean
{
return x.length > value.length
}
)
}
public static function wrap( f:Function):Function
{
return(
function( x : *, index : int, array : Array ) : *
{
return f( x )
}
)
} |
Partager