Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const join = (iterable, separator = '') => {
- if (iterable === undefined) return undefined;
- let str = '';
- for (let pos = 0, last = iterable.length - 1; pos <= last; pos++)
- str += iterable[pos] + (pos < last ? separator : '');
- return str;
- };
- const recursiveSort = (function() {
- let referencePoint = 0,
- position,
- endOfIterable,
- tempArray,
- sortTestFn,
- typeOfReturn;
- const builtIn = (a, b) =>
- typeof a === 'string' || typeof b === 'string' ? +(a > b) : a - b;
- return function innerFn(iterable, callBackFn = builtIn) {
- if (referencePoint === 0) {
- if (iterable === undefined) return undefined;
- position = referencePoint;
- endOfIterable = (tempArray = [...iterable]).length - 1;
- sortTestFn = callBackFn;
- typeOfReturn = typeof iterable;
- }
- if (referencePoint >= endOfIterable) {
- referencePoint = 0;
- return typeOfReturn === 'string' ? join(tempArray) : tempArray;
- }
- if (sortTestFn(tempArray[position], tempArray[position + 1]) >= 1) {
- [tempArray[position], tempArray[position + 1]] = [tempArray[position + 1], tempArray[position]];
- if (position > 0 && sortTestFn(tempArray[position - 1], tempArray[position]) >= 1) {
- position--;
- return innerFn();
- }
- }
- position = ++referencePoint;
- return innerFn();
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement