File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * JS sort function using ES5 syntax
3+ * @param {array } myArray - Array variable which has to be sorted based on a custom logic
4+ * @return {array } - The sorted array
5+ */
6+ sortArrayUsingEs5 : function ( myArray ) {
7+
8+ // Need to call the sort function of array object
9+ return myArray . sort ( function ( itemA , itemB ) {
10+ /**
11+ * The sorting logic comes here
12+ * @param {object } itemA - The first element for comparison. Will never be undefined.
13+ * @param {object } itemB - The second element for comparison. Will never be undefined.
14+ *
15+ * @return {number } - Nedd to return three different numbers based on the following logic:
16+ * If itemA is sorted before itemB, then the return value will be -1 (or any other negative number)
17+ * If itemB is sorted before itemA, then the return value will be 1 (or any other positive number)
18+ * If itemA equals with itemB, then the return value will be 0
19+ *
20+ * itemA and itemB can be a complex object as well, the sorting logic can be based on any attributes of the main objects.
21+ */
22+
23+ if ( /* Logic here which checks that itemA is sorted before itemB */ itemA < itemB )
24+ return - 1 ;
25+
26+ if ( /* Logic here which checks that itemB is sorted before itemA */ itemA > itemB )
27+ return 1 ;
28+
29+ // Otherwise
30+ return 0 ;
31+ } ) ;
32+ } ,
Original file line number Diff line number Diff line change 1+ /**
2+ * JS sort function using ES6+ syntax
3+ * @param {array } myArray - Array variable which has to be sorted based on a custom logic
4+ * @return {array } - The sorted array
5+ */
6+ sortArrayUsingEs6Plus : function ( myArray ) {
7+ // Need to call the sort function of array object
8+ return ( myArray . sort ( ( itemA , itemB ) => {
9+ /**
10+ * The sorting logic comes here
11+ * @param {object } itemA - The first element for comparison. Will never be undefined.
12+ * @param {object } itemB - The second element for comparison. Will never be undefined.
13+ *
14+ * @return {number } - Nedd to return three different numbers based on the following logic:
15+ * If itemA is sorted before itemB, then the return value will be -1 (or any other negative number)
16+ * If itemB is sorted before itemA, then the return value will be 1 (or any other positive number)
17+ * If itemA equals with itemB, then the return value will be 0
18+ *
19+ * itemA and itemB can be a complex object as well, the sorting logic can be based on any attributes of the main objects.
20+ */
21+
22+ if ( /* Logic here which checks that itemA is sorted before itemB */ itemA < itemB )
23+ return - 1 ;
24+
25+ if ( /* Logic here which checks that itemB is sorted before itemA */ itemA > itemB )
26+ return 1 ;
27+
28+ // Otherwise
29+ return 0 ;
30+ } ) ) ;
31+ } ,
You can’t perform that action at this time.
0 commit comments