Skip to content

Commit 14b1dd2

Browse files
committed
fix(group): order groups by original ordering of first item in each group
1 parent 50cb59c commit 14b1dd2

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

index.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,23 +385,20 @@ export function keyByFn<T>(f: (element: T) => string): (array: ArrayLike<T>) =>
385385
}
386386

387387
export function group<T>(array: ArrayLike<T>, compare: Comparator<T>): T[][] {
388-
return copy(array)
389-
.sort(compare)
390-
.reduce((groups, element) => {
391-
if (groups.length === 0) {
392-
return [[element]];
388+
const result: T[][] = [];
389+
390+
for (let i = 0; i < array.length; ++i) {
391+
for (let j = 0; j < result.length; ++j) {
392+
if (compare(result[j][0], array[i]) === Comparison.equal) {
393+
result[j].push(array[i]);
394+
break;
393395
}
396+
}
394397

395-
const group = groups[groups.length - 1];
396-
const exemplar = group[0];
398+
result.push([array[i]]);
399+
}
397400

398-
if (compare(exemplar, element) === Comparison.equal) {
399-
return groups.slice(0, groups.length - 1)
400-
.concat([group.concat([element])]);
401-
} else {
402-
return groups.concat([[element]]);
403-
}
404-
}, [] as T[][]);
401+
return result;
405402
}
406403

407404
export function groupFn<T>(compare: Comparator<T>): (array: ArrayLike<T>) => T[][] {

0 commit comments

Comments
 (0)