diff --git a/VERSIONS b/VERSIONS index 6107b192e..2072ede2e 100644 --- a/VERSIONS +++ b/VERSIONS @@ -45,6 +45,8 @@ ChucK VERSIONS log are distinguished from windows drive letters (e.g., "A:\") - (fixed) Type.of() now reports more specific array type (e.g., `string[]`) instead of generic `@array` +- (updated) calling .sort() on an array of strings will sort by string + (instead of Object pointers) - (updated, internal) dynamic array types are now cached and reused, preventing potential memory build-up across compilations diff --git a/src/core/chuck_oo.cpp b/src/core/chuck_oo.cpp index e2559ebc4..443ad82cd 100644 --- a/src/core/chuck_oo.cpp +++ b/src/core/chuck_oo.cpp @@ -924,16 +924,55 @@ static bool ck_compare_sint( t_CKUINT lhs, t_CKUINT rhs ) // sort by 2-norm / magnitude return (t_CKINT)lhs < (t_CKINT)rhs; } + + + + +//----------------------------------------------------------------------------- +// name: ck_compare_string() +// desc: compare function for sorting uints as chuck strings +//----------------------------------------------------------------------------- +static bool ck_compare_string( t_CKUINT lhs, t_CKUINT rhs ) +{ + const std::string& lhs_str = ((Chuck_String*)lhs)->str(); + const std::string& rhs_str = ((Chuck_String*)rhs)->str(); + + return lhs_str.compare(rhs_str) < 0; +} + + + + //----------------------------------------------------------------------------- // name: sort() // desc: sort the array in ascending order //----------------------------------------------------------------------------- void Chuck_ArrayInt::sort() { - // if object references sort as unsigned - if( m_is_obj ) std::sort( m_vector.begin(), m_vector.end() ); - // if not object references, sort as signed ints - else std::sort( m_vector.begin(), m_vector.end(), ck_compare_sint ); + // check size + if( size() == 0 ) return; + + // if object references | 1.5.3.5 (azaday) added + if( m_is_obj ) + { + // if this is a string[] + if( this->type_ref->array_depth == 1 && this->type_ref->base_name == "string" ) + { + // sort as string array + std::sort( m_vector.begin(), m_vector.end(), ck_compare_string ); + } + else // not string object array + { + // sort object pointers as unsigned ints + std::sort( m_vector.begin(), m_vector.end() ); + } + } + // if not object references + else + { + // sort as signed ints + std::sort( m_vector.begin(), m_vector.end(), ck_compare_sint ); + } } diff --git a/src/test/01-Basic/175-array-sort.ck b/src/test/01-Basic/175-array-sort.ck index 198f234ff..e7edd223e 100644 --- a/src/test/01-Basic/175-array-sort.ck +++ b/src/test/01-Basic/175-array-sort.ck @@ -63,3 +63,12 @@ for( auto x : arrayVec3 ) { cherr <= x <= " "; } cherr <= IO.nl(); arrayVec4.sort(); // print for( auto x : arrayVec4 ) { cherr <= x <= " "; } cherr <= IO.nl(); + +//----------------------------------------------------------------------------- +// string array: sort alphabetically +//----------------------------------------------------------------------------- +[ "ge", "andrew", "nick", ] @=> string arrString[]; +// sort by ascending value +arrString.sort(); +// print +for( auto x : arrString ) { cherr <= x <= " "; } cherr <= IO.nl(); diff --git a/src/test/01-Basic/175-array-sort.txt b/src/test/01-Basic/175-array-sort.txt index 12cb3f4c1..c8eb54f62 100644 --- a/src/test/01-Basic/175-array-sort.txt +++ b/src/test/01-Basic/175-array-sort.txt @@ -1,7 +1,8 @@ --2 -1 0 1 2 3 4 5 -5 4 3 2 1 0 -1 -2 --3.14159 1 2 3.14159 -#(0,0) #(1,0) #(-1,-1) #(1,1) -%(1,3.14159) %(2,1.5708) %(2,3.14159) -@(0,0,0) @(-1,-1,-1) @(1,2,1) -@(0,0,0,0) @(-1,-1,-1,-1) @(1,3,1,-2) +-2 -1 0 1 2 3 4 5 +5 4 3 2 1 0 -1 -2 +-3.14159 1 2 3.14159 +#(0,0) #(1,0) #(-1,-1) #(1,1) +%(1,3.14159) %(2,1.5708) %(2,3.14159) +@(0,0,0) @(-1,-1,-1) @(1,2,1) +@(0,0,0,0) @(-1,-1,-1,-1) @(1,3,1,-2) +andrew ge nick