Skip to content

Commit a5e4368

Browse files
authored
New editor macro: array sorting function (#161)
* Create arraySortEs5.js * Create arraySortEs6.js
1 parent 7eb9ad9 commit a5e4368

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

General/arraySortEs5.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
},

General/arraySortEs6.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
},

0 commit comments

Comments
 (0)