-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringSort.js
103 lines (90 loc) · 2.82 KB
/
stringSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function internalSort(indexArray, stringArray, internalComp) {
if (indexArray.length == 1) {
return indexArray;
}
const midpt = parseInt(indexArray.length / 2, 10);
const grandMerge = [];
const leftAr = indexArray.slice(0, midpt);
const rightAr = indexArray.slice(midpt, indexArray.length);
let sortedLeft = internalSort(leftAr, stringArray, internalComp);
let sortedRight = internalSort(rightAr, stringArray, internalComp);
while (sortedLeft.length && sortedRight.length) {
if (internalComp(stringArray[sortedLeft[0]], stringArray[sortedRight[0]]) <= 0) {
grandMerge.push(sortedLeft[0]);
sortedLeft = sortedLeft.slice(1);
} else {
grandMerge.push(sortedRight[0]);
sortedRight = sortedRight.slice(1);
}
}
while (sortedLeft.length) {
grandMerge.push(sortedLeft[0]);
sortedLeft = sortedLeft.slice(1);
}
while (sortedRight.length) {
grandMerge.push(sortedRight[0]);
sortedRight = sortedRight.slice(1);
}
// delete leftAr; delete rightAr;
return grandMerge;
}
/**
* Returns index wise sorted array for a string array
*
* @param {object[]} stringArray an Array of Objects
* @param {boolean} byIndex set true if you want results returned by index
* @param {Function} compartorFn implement your custom comparatorFn here !
* @returns {object[]} Array of index/object in sorted fashion
*/
function sort(stringArray, byIndex = false, compartorFn = null) {
if (stringArray.length < 1) return [];
const internalComp = compartorFn === null ? (x, y) => {
let i = 0;
while (i < x.length && i < y.length) {
if (x.charCodeAt(i) != y.charCodeAt(i)) {
return (x.charCodeAt(i) - y.charCodeAt(i));
}
i++;
}
return (x.length - y.length);
} : compartorFn;
const indexArray = Array(stringArray.length);
for (let i = 0; i < stringArray.length; i++) indexArray[i] = i;
const result = internalSort(indexArray, stringArray, internalComp);
if (byIndex) {
return result;
}
const valueResult = [];
for (let i = 0; i < result.length; i++) {
valueResult.push(stringArray[result[i]]);
}
return valueResult;
}
/**
*
* @param {object[]} array an Array of Objects
* @param {Function} compartorFn comparator function
*
* @returns {boolean} states whether the array is sorted or not
*/
function sorted(array, compartorFn = null) {
if (array.length < 2) return true;
const internalComp = compartorFn === null ? (x, y) => {
let i = 0;
while (i < x.length && i < y.length) {
if (x.charCodeAt(i) != y.charCodeAt(i)) {
return (x.charCodeAt(i) - y.charCodeAt(i));
}
i++;
}
return (x.length - y.length);
} : compartorFn;
for (let x = 0; x < array.length - 1; x++) {
if (internalComp(array[x], array[x + 1]) > 0) return false;
}
return true;
}
module.exports = {
sort,
sorted,
};