File tree Expand file tree Collapse file tree 1 file changed +11
-14
lines changed
Expand file tree Collapse file tree 1 file changed +11
-14
lines changed Original file line number Diff line number Diff line change @@ -385,23 +385,20 @@ export function keyByFn<T>(f: (element: T) => string): (array: ArrayLike<T>) =>
385385}
386386
387387export function group < T > ( array : ArrayLike < T > , compare : Comparator < T > ) : T [ ] [ ] {
388- return copy ( array )
389- . sort ( compare )
390- . reduce ( ( groups , element ) => {
391- if ( groups . length === 0 ) {
392- return [ [ element ] ] ;
388+ const result : T [ ] [ ] = [ ] ;
389+
390+ for ( let i = 0 ; i < array . length ; ++ i ) {
391+ for ( let j = 0 ; j < result . length ; ++ j ) {
392+ if ( compare ( result [ j ] [ 0 ] , array [ i ] ) === Comparison . equal ) {
393+ result [ j ] . push ( array [ i ] ) ;
394+ break ;
393395 }
396+ }
394397
395- const group = groups [ groups . length - 1 ] ;
396- const exemplar = group [ 0 ] ;
398+ result . push ( [ array [ i ] ] ) ;
399+ }
397400
398- if ( compare ( exemplar , element ) === Comparison . equal ) {
399- return groups . slice ( 0 , groups . length - 1 )
400- . concat ( [ group . concat ( [ element ] ) ] ) ;
401- } else {
402- return groups . concat ( [ [ element ] ] ) ;
403- }
404- } , [ ] as T [ ] [ ] ) ;
401+ return result ;
405402}
406403
407404export function groupFn < T > ( compare : Comparator < T > ) : ( array : ArrayLike < T > ) => T [ ] [ ] {
You can’t perform that action at this time.
0 commit comments