## 概要
```txt
bbb
ccc
aaa
```
のようなテキストを選択して
```txt
aaa
bbb
ccc
```
のようにソートしたい。
## ソリューション
[[コミュニティプラグイン]]をインストールする方法と[[Templater]]を使う方法があります。
### [[コミュニティプラグイン]]をインストールする方法
[[Sort & Permute lines]]をインストールし、`Sort alphabetically`コマンドを実行します。他にもいくつかのソートコマンドがあります。
<div class="link-card">
<div class="link-card-header">
<img src="https://github.githubassets.com/favicons/favicon.svg" class="link-card-site-icon"/>
<span class="link-card-site-name">GitHub</span>
</div>
<div class="link-card-body">
<div class="link-card-content">
<div>
<p class="link-card-title">GitHub - Vinzent03/obsidian-sort-and-permute-lines: Sort and Permute lines in whole file or selection.</p>
</div>
<div class="link-card-description">
Sort and Permute lines in whole file or selection. - GitHub - Vinzent03/obsidian-sort-and-permute-li...
</div>
</div>
<img src="https://opengraph.githubassets.com/6104f825fea235f7e3ab84b081b3ef36f2fef859300e152282360a394d4c7678/Vinzent03/obsidian-sort-and-permute-lines" class="link-card-image" />
</div>
<a href="https://github.com/Vinzent03/obsidian-sort-and-permute-lines"></a>
</div>
### [[Templater]]を使う方法
以下のようなスクリプトを作成し、コマンドで実行します。`sorter`に指定する関数を変更することで、ソートロジックを変更できます。
```js
<%*
function sorter(toOrdered, order = "asc") {
return (a, b) =>
order === "asc"
? toOrdered(a) > toOrdered(b)
? 1
: toOrdered(b) > toOrdered(a)
? -1
: 0
: toOrdered(a) < toOrdered(b)
? 1
: toOrdered(b) < toOrdered(a)
? -1
: 0;
};
const editor = app.workspace.activeLeaf.view.editor
const r = editor.getSelection()
.split("\n")
.sort(sorter((x) => x))
.join("\n")
editor.replaceSelection(r)
%>
```
> [!info] コマンドにホットキーを登録する方法
> [[Templaterで作成したコマンド(スクリプト)をホットキーで実行する方法]] を参考にしてください。