# 各行按字母排序

> 将各行按 A 到 Z 排序，不区分大小写和变音符号

版本：1.0.0 · 作者 Deck

- 安装到 Deck：deck://install-plugin?id=sort-lines&url=https%3A%2F%2Fgithub.com%2Fyuzeguitarist%2Fdeck-plugins%2Freleases%2Fdownload%2Fplugins%2Fsort-lines-1.0.0.zip
- 下载 .zip：https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/sort-lines-1.0.0.zip
- 在 GitHub 上查看：https://github.com/yuzeguitarist/deck-plugins/tree/main/plugins/sort-lines

也可以用终端安装：

```sh
mkdir -p ~/.deck/scripts/sort-lines && curl -fsSL https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/sort-lines-1.0.0.zip | tar -xf - -C ~/.deck/scripts/sort-lines
```

## 脚本

```js
// Sorts the lines from A to Z, ignoring case and accents. Lines that compare equal keep their order.
function transform(input) {
  const text = String(input ?? "").replace(/\r\n?/g, "\n");
  if (text === "") return "";
  const end = text.endsWith("\n") ? "\n" : "";
  const lines = (end ? text.slice(0, -1) : text).split("\n");
  // NFD splits accents off their letters so they can be dropped. Comparing code units instead of using
  // locale rules gives the same order in every JavaScript engine.
  const keys = lines.map((line) => line.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase());
  const order = lines.map((line, index) => index);
  order.sort((a, b) => (keys[a] < keys[b] ? -1 : keys[a] > keys[b] ? 1 : a - b));
  return order.map((index) => lines[index]).join("\n") + end;
}
```

---

- HTML：https://deckclip.app/zh-cn/plugins/sort-lines
- English：https://deckclip.app/plugins/sort-lines.md

© 2026 潘禹泽
