File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -588,6 +588,27 @@ var _ = {};
588
588
// of that string. For example, _.sortBy(people, 'name') should sort
589
589
// an array of people by their name.
590
590
_ . sortBy = function ( collection , iterator ) {
591
+ //apply iterator to collection
592
+
593
+ if ( typeof iterator === 'string' ) {
594
+ return collection . sort ( function ( a , b ) {
595
+ return ( a [ iterator ] - b [ iterator ] ) ;
596
+ } ) ;
597
+ } else {
598
+ var iterated = [ ] ;
599
+ var newCollection = [ ] ;
600
+ _ . each ( collection , function ( val ) {
601
+ iterated . push ( [ val , iterator ( val ) ] ) ;
602
+ } ) ;
603
+
604
+ iterated . sort ( function ( a , b ) {
605
+ return a [ 1 ] - b [ 1 ] ;
606
+ } ) ;
607
+ _ . each ( iterated , function ( val ) {
608
+ newCollection . push ( val [ 0 ] ) ;
609
+ } ) ;
610
+ return newCollection ;
611
+ }
591
612
} ;
592
613
593
614
// Zip together two or more arrays with elements of the same index
You can’t perform that action at this time.
0 commit comments