"use strict";

module.exports = class SortableSet extends Set {

	constructor(initialIterable, defaultSort) {
		super(initialIterable);
		this._sortFn = defaultSort;
	}

	/**
	 * @param {Function} sortFn - function to sort the set
	 * @returns {void}
	 */
	sortWith(sortFn) {
		const sortedArray = Array.from(this).sort(sortFn);
		this.clear();
		for(let i = 0; i < sortedArray.length; i += 1) {
			this.add(sortedArray[i]);
		}
	}

	/**
	 * @returns {void}
	 */
	sort() {
		this.sortWith(this._sortFn);
	}
};

@@ -164,116 +164,620 @@
 t;%0A%09
-%7D%0A%0A%09/**%0A%09 * @param %7BFunction%7D sortFn - function to sort the set%0A%09 * @returns %7Bvoid%7D%0A%09 */%0A%09sortWith(sortFn) %7B
+%09this._lastActiveSortFn = null;%0A%09%09this._isSorted = false;%0A%09%7D%0A%0A%09/**%0A%09 * @param %7Bany%7D value - value to add to set%0A%09 * @returns %7BSortableSet%7D - returns itself%0A%09 */%0A%09add(value) %7B%0A%09%09this._lastActiveSortFn = null;%0A%09%09this._isSorted = false;%0A%09%09super.add(value);%0A%09%09return this;%0A%09%7D%0A%0A%09/**%0A%09 * @returns %7Bvoid%7D%0A%09 */%0A%09clear() %7B%0A%09%09this._lastActiveSortFn = null;%0A%09%09this._isSorted = false;%0A%09%09super.clear();%0A%09%7D%0A%0A%09/**%0A%09 * @param %7BFunction%7D sortFn - function to sort the set%0A%09 * @returns %7Bvoid%7D%0A%09 */%0A%09sortWith(sortFn) %7B%0A%09%09if(this._isSorted && sortFn === this._lastActiveSortFn) %7B%0A%09%09%09// already sorted - nothing to do%0A%09%09%09return;%0A%09%09%7D%0A
 %0A%09%09c
@@ -828,20 +828,21 @@
 tFn);%0A%09%09
-this
+super
 .clear()
@@ -927,16 +927,76 @@
 %5D);%0A%09%09%7D%0A
+%09%09this._lastActiveSortFn = sortFn;%0A%09%09this._isSorted = true;%0A
 %09%7D%0A%0A%09/**

optimize sortableSet to prevent unnecessary sorting