We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6a48af1 commit 0a40ae4Copy full SHA for 0a40ae4
src/arrays.ts
@@ -49,10 +49,15 @@ export function uniqueArray<T>(array: T[]): T[] {
49
* Перемешивает исходный массив и возвращает новый
50
*/
51
export function shuffleArray<T>(array: T[]): T[] {
52
- return array
53
- .map<[number, T]>((a) => [Math.random(), a])
54
- .sort((a, b) => a[0] - b[0])
55
- .map<T>((a) => a[1]);
+ const result = array.slice();
+
+ for (let i = result.length - 1; i > 0; i--) {
+ const j = Math.floor(Math.random() * (i + 1));
56
57
+ [result[i], result[j]] = [result[j], result[i]];
58
+ }
59
60
+ return result;
61
}
62
63
/**
0 commit comments