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