自然数ソート

提供: MeryWiki
ナビゲーションに移動 検索に移動

概要[編集]

自然順ソート(Natural Sort)します。

機能[編集]

アスキー順ではなく、自然順でソートする。

自然順ソート:1, 2, 3, ..., 9, 10
アスキー順ソート:1, 10, 2, 3, ...., 9, 10

変更履歴[編集]

  • 1.0.0 (2023-11-19)
    • 初版

ソースコード[編集]

昇順用[編集]

#title = "自然順で、昇順に並べ替え"
#tooltip = "自然順(Natural Sort order)で、選択範囲または文書全体を昇順に並べ替えます。"
#begingroup = true
/*
Copyright (c) Kuro. All rights reserved.
Licensed under the MIT License.

Natural Sort prioritizes human intuition, considering numeric values 
for an intuitive string order. 
Enhances user experience in sorting textual data like Windows Explorer.

 exalmple  (1, 2, 10) instead of (1, 10, 2)
*/

function naturalSortKey(s) {
	return s.toLowerCase().split(/(\d+)/).map(function (text) {
		return isNaN(text) ? text : parseInt(text);
	});
}

function naturalSort(lines) {
	lines.sort(function (a, b) {
		var regEx = /(\d+)|(\D+)/g;
		var tokensA = a.match(regEx) || [];
		var tokensB = b.match(regEx) || [];

		while (tokensA.length > 0 && tokensB.length > 0) {
			var tokenA = tokensA.shift();
			var tokenB = tokensB.shift();

			var numA = parseInt(tokenA, 10);
			var numB = parseInt(tokenB, 10);

			if (!isNaN(numA) && !isNaN(numB)) {
				if (numA < numB) return -1;
				if (numA > numB) return 1;
			} else {
				if (tokenA < tokenB) return -1;
				if (tokenA > tokenB) return 1;
			}
		} 
		return tokensA.length - tokensB.length;
	});
}

var sel = document.selection;

if (sel.Text == "") {
	sel.SelectAll();
}

var lines = sel.Text.replace(/\n?$/, "").split("\n")
naturalSort(lines);
sel.Text=lines.join("\n");

降順用[編集]

#title = "自然順で、降順に並べ替え"
#tooltip = "自然順(Natural Sort order)で、選択範囲または文書全体を降順に並べ替えます。"
#begingroup = true
/*
Copyright (c) Kuro. All rights reserved.
Licensed under the MIT License.

Natural Sort prioritizes human intuition, considering numeric values 
for an intuitive string order. 
Enhances user experience in sorting textual data like Windows Explorer.

 exalmple  (1, 2, 10) instead of (1, 10, 2)
*/

function naturalSortKey(s) {
	return s.toLowerCase().split(/(\d+)/).map(function (text) {
		return isNaN(text) ? text : parseInt(text);
	});
}

function naturalSort(lines) {
	lines.sort(function (a, b) {
		var regEx = /(\d+)|(\D+)/g;
		var tokensA = a.match(regEx) || [];
		var tokensB = b.match(regEx) || [];

		while (tokensA.length > 0 && tokensB.length > 0) {
			var tokenA = tokensA.shift();
			var tokenB = tokensB.shift();

			var numA = parseInt(tokenA, 10);
			var numB = parseInt(tokenB, 10);

			if (!isNaN(numA) && !isNaN(numB)) {
				if (numA > numB) return -1;
				if (numA < numB) return 1;
			} else {
				if (tokenA > tokenB) return -1;
				if (tokenA < tokenB) return 1;
			}
		}

		return tokensB.length - tokensA.length;
	});
}
// Example usage:

var sel = document.selection;

if (sel.Text == "") {
	sel.SelectAll();
}

var lines = sel.Text.replace(/\n?$/, "").split("\n")
naturalSort(lines);
sel.Text=lines.join("\n");

謝辞[編集]

重複行の削除マクロ(RemoveDuplicates.js)の処理速度についての、Noah さんの投稿 (2023年10月21日) への投稿をもとに、ChatGPT で、ざっと作りました。

スポンサーリンク