namespace Generics { function swap < T > ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } function sortHelper < T > ( arr , callbackfn ? ) { if ( arr . length <= 0 || ! callbackfn ) { return arr ; } let len = arr . length ; for ( let i = 0 ; i < len - 1 ; ++ i ) { for ( let j = i + 1 ; j < len ; ++ j ) { if ( callbackfn ( arr [ i ] , arr [ j ] ) > 0 ) { swap ( arr , i , j ) ; } } } return arr ; } export function arraySort < T > ( arr , callbackfn ? ) { return sortHelper ( arr , callbackfn ) ; } } function testGenerics ( ) { msg ( "testGenerics" ) ; let inArray = [ 4 , 3 , 4593 , 23 , 43 , - 1 ] ; Generics . arraySort ( inArray , ( x , y ) => { return x - y ; } ) ; let expectedArray = [ - 1 , 3 , 4 , 23 , 43 , 4593 ] ; for ( let i = 0 ; i < expectedArray . length ; i ++ ) { assert ( inArray [ i ] == expectedArray [ i ] ) ; } } testGenerics ( ) ;