File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,16 @@ class Helper {
99 return Array . isArray ( arr ) ? true : this . _throw ( ) ;
1010 }
1111
12+ // To check if arr has only nums.
13+ static is_num_array ( arr ) {
14+ var a = arr . reduce ( function ( result , val ) {
15+ return result && typeof val === 'number' ;
16+ } , true ) ;
17+ if ( a == false ) {
18+ throw new TypeError ( "Must be an array of numbers!" )
19+ }
20+ }
21+
1222 // To get the head or first element of the array.
1323 static head ( arr ) {
1424 this . is_array ( arr ) ;
@@ -123,6 +133,16 @@ class Helper {
123133 throw new TypeError ( "Input parameters not valid!" ) ;
124134 }
125135 }
136+ //To sort numeric arrays ascending and descending
137+ static sort_nums ( arr , reverse = false ) {
138+ this . is_array ( arr ) ;
139+ this . is_num_array ( arr ) ;
140+ if ( reverse ) {
141+ return arr . sort ( function ( a , b ) { return b - a } ) ;
142+ } else {
143+ return arr . sort ( function ( a , b ) { return a - b } ) ;
144+ }
145+ }
126146}
127147
128148export default Helper ;
You can’t perform that action at this time.
0 commit comments