hobord
2/4/2016 - 2:53 PM

js sort

js sort

class Sorter {
	key: string;
	direction: number;

	constructor() {
		this.direction = 1;
	}

	sort(key, data) {
		if(this.key === key) {
			this.direction = this.direction * -1;
		}
		else {
			this.direction = 1;	
		}
		this.key = key;

		data.sort((a,b) => {
			if(a[key] === b[key]) {
				return 0;
			}
			else if(a[key] > b[key]) {
				return 1 * this.direction;
			}
			else {
				return -1 * this.direction;
			}
		});
	}
}