We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2ba0499 commit 0eaa847Copy full SHA for 0eaa847
1 file changed
lib/util/SortableSet.js
@@ -0,0 +1,28 @@
1
+"use strict";
2
+
3
+module.exports = class SortableSet extends Set {
4
5
+ constructor(initialIterable, defaultSort) {
6
+ super(initialIterable);
7
+ this._sortFn = defaultSort;
8
+ }
9
10
+ /**
11
+ * @param {Function} sortFn - function to sort the set
12
+ * @returns {void}
13
+ */
14
+ sortWith(sortFn) {
15
+ const sortedArray = Array.from(this).sort(sortFn);
16
+ this.clear();
17
+ for(let i = 0; i < sortedArray.length; i += 1) {
18
+ this.add(sortedArray[i]);
19
20
21
22
23
24
25
+ sort() {
26
+ this.sortWith(this._sortFn);
27
28
+};
0 commit comments