Skip to content

useSortedKeys

¥Summary

¥How to enable in your editor

.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.action.useSortedKeys.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}

¥How to configure

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": "on"
}
}
}
}

¥Description

按自然顺序对 JSON 对象的键进行排序。

¥Sort the keys of a JSON object in natural order.

自然顺序 表示大写字母在前,小写字母在后(例如 A < a < B < b),数字的比较方式与人类的比较方式相同(例如 9 < 10)。

¥Natural order means that uppercase letters come before lowercase letters (e.g. A < a < B < b) and numbers are compared in a human way (e.g. 9 < 10).

¥Examples

{
"vase": "fancy",
"nested": {
"omega": "bar",
"alpha": "foo"
}
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Source action diff:

1 1 {
2 - ····vase:·fancy,
3 - ····nested:·{
2+ ····nested:·{
4 3 “omega”: “bar”,
5 4 “alpha”: “foo”
6 - ····}
5+ ····},
6+ ····vase:·fancy
7 7 }
8 8

code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Source action diff:

2 2 “vase”: “fancy”,
3 3 “nested”: {
4 - ········omega:·bar,
5 - ········alpha:·foo
4+ ········alpha:·foo,
5+ ········omega:·bar
6 6 }
7 7 }

¥Options

此操作接受以下选项

¥This actions accepts following options

此选项支持 naturallexicographic 值。natural 是默认设置。

¥This options supports natural and lexicographic values. Where as natural is the default.

以下将应用自然排序。

¥Following will apply the natural sort order.

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": {
"options": {
"sortOrder": "natural"
}
}
}
}
}
}
{
"val13": 1,
"val1": 1,
"val2": 1,
"val21": 1,
"val11": 1
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Source action diff:

1 1 {
2 - ····val13:·1,
3 - ····val1:·1,
4 - ····val2:·1,
5 - ····val21:·1,
6 - ····val11:·1
2+ ····val1:·1,
3+ ····val2:·1,
4+ ····val11:·1,
5+ ····val13:·1,
6+ ····val21:·1
7 7 }
8 8

以下将应用字典序排序。

¥Following will apply the lexicographic sort order.

biome.json
{
"assist": {
"actions": {
"source": {
"useSortedKeys": {
"options": {
"sortOrder": "lexicographic"
}
}
}
}
}
}
{
"val13": 1,
"val1": 1,
"val2": 1,
"val21": 1,
"val11": 1
}
code-block.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Source action diff:

1 1 {
2 - ····val13:·1,
3 - ····val1:·1,
4 - ····val2:·1,
5 - ····val21:·1,
6 - ····val11:·1
2+ ····val1:·1,
3+ ····val11:·1,
4+ ····val13:·1,
5+ ····val2:·1,
6+ ····val21:·1
7 7 }
8 8

¥Related links