<commit_before>export function sortIndexFactory(model) {
	return columns => {
		const columnListState = model.columnList();

		const listIndex = columnListState.index;
		const templateIndex = columnListState.columns.map(c => c.key);
		const viewIndex = columns.map(c => c.key);

		const sort = sortFactory(listIndex, templateIndex, viewIndex);
		const left = sort(columns.filter(c => c.pin === 'left'));
		const center = sort(columns.filter(c => !c.pin));
		const right = sort(columns.filter(c => c.pin === 'right'));

		const index = left.concat(center).concat(right);
		return {
			index: index,
			hasChanges: !equals(listIndex, index)
		};
	};
}

function sortFactory(listIndex, templateIndex, viewIndex) {
	const compare = compareFactory(listIndex, templateIndex, viewIndex);
	return columns => {
		const columnIndex = Array.from(columns);
		columnIndex.sort(compare);

		return columnIndex.map(c => c.key);
	};
}

function compareFactory(listIndex, templateIndex, viewIndex) {
	const listFind = findFactory(listIndex);
	const templateFind = findFactory(templateIndex);
	const viewFind = findFactory(viewIndex);

	return (x, y) => {
		const xKey = x.key;
		const yKey = y.key;

		let xi = x.class === 'data' ? listFind(xKey) : viewFind(xKey);
		let yi = x.class === 'data' ? listFind(yKey) : viewFind(yKey);

		if (xi === yi) {
			xi = x.index;
			yi = y.index;

			if (xi === yi) {
				xi = viewFind(xKey);
				yi = viewFind(yKey);

				if (xi === yi) {
					xi = templateFind(xKey);
					yi = templateFind(yKey);
				}
			}
		}

		return yi === -1 ? -1 : xi === -1 ? 1 : xi - yi;
	};
}

function findFactory(index) {
	const map =
		index.reduce((memo, key, i) => {
			memo.set(key, i);
			return memo;
		}, new Map());

	return key => map.has(key) ? map.get(key) : -1;
}

function equals(xs, ys) {
	const length = xs.length;
	if (length !== ys.length) {
		return false;
	}

	for (let i = 0; i < length; i++) {
		if (xs[i] !== ys[i]) {
			return false;
		}
	}
	return true;
}<commit_msg>column sort on scores<commit_after>export function sortIndexFactory(model) {
	return columns => {
		const columnListState = model.columnList();

		const listIndex = columnListState.index;
		const templateIndex = columnListState.columns.map(c => c.key);
		const viewIndex = columns.map(c => c.key);

		const sort = sortFactory(listIndex, templateIndex, viewIndex);
		const left = sort(columns.filter(c => c.pin === 'left'));
		const center = sort(columns.filter(c => !c.pin));
		const right = sort(columns.filter(c => c.pin === 'right'));

		const index = left.concat(center).concat(right);
		return {
			index: index,
			hasChanges: !equals(listIndex, index)
		};
	};
}

function sortFactory(listIndex, templateIndex, viewIndex) {
	const compare = compareFactory(listIndex, templateIndex, viewIndex);
	return columns => {
		const columnIndex = Array.from(columns);
		columnIndex.sort(compare);

		return columnIndex.map(c => c.key);
	};
}

function compareFactory(listIndex, templateIndex, viewIndex) {
	const listFind = findFactory(listIndex);
	const templateFind = findFactory(templateIndex);
	const viewFind = findFactory(viewIndex);

	const weightCache = {};
	const getWeight = column => {
		const key = column.key;
		if (weightCache.hasOwnProperty(key)) {
			return weightCache[key];
		}

		const candidates = [
			listFind(key) + (column.class === 'data' ? 0.9 : 0.7),
			column.index + 0.8,
			viewFind(key) + (column.class !== 'data' ? 0.9 : 0.7),
			templateFind(key) + 0.6
		];

		const weights = candidates.filter(w => w >= 0);
		const weight = weights.length ? weights[0] : -1;
		weightCache[key] = weight;

		return weight;
	};

	return (x, y) => {
		const xi = getWeight(x);
		const yi = getWeight(y);

		return yi === -1 ? -1 : xi === -1 ? 1 : xi - yi;
	};
}

function findFactory(index) {
	const map =
		index.reduce((memo, key, i) => {
			memo.set(key, i);
			return memo;
		}, new Map());

	return key => map.has(key) ? map.get(key) : -1;
}

function equals(xs, ys) {
	const length = xs.length;
	if (length !== ys.length) {
		return false;
	}

	for (let i = 0; i < length; i++) {
		if (xs[i] !== ys[i]) {
			return false;
		}
	}
	return true;
}
{'fraction_non_alphanumeric': 0.1641395908543923, 'fraction_numerical': 0.006498194945848376, 'mean_word_length': 3.6435754189944136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1998198', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1453, 'n_words': 356}