Skip to content

Commit 83ac2da

Browse files
committed
Support sorting in reverse order (#225)
1 parent 93be9f2 commit 83ac2da

6 files changed

Lines changed: 33 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
# 6.6.0 - Oct 28, 2024
3+
✨ Feature
4+
* (refs [#225](https://github.com/pascalre/vscode-yaml-sort/issues/225)) Support sorting in reverse order
5+
26
# 6.5.18 - Oct 28, 2024
37
📦 Dependencies
48
* Update dependencies to latest versions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This extension contributes the following settings:
4141
| `schema` | Schema to use. Possible values are `HOMEASSISTANT_SCHEMA`, `CLOUDFORMATION_SCHEMA`, `CORE_SCHEMA`, `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`. | `DEFAULT_SCHEMA` |
4242
| `sortArrays` | When `true`, will sort arrays | `false` |
4343
| `sortOnSave` | When `0`, will sort files when saving document. When `1`, `2` or `3`, will use customSortKeywords. Set to negative value to disable sortOnSave. Only works in combination with `editor.formatOnSave` set to `true`. | `0` |
44+
| `sortOrderReverse` | When `true`, will sort in reverse order | `false` |
4445
| `useAsFormatter` | When `true`, will enable default YAML formatter (requires restart). | `false` |
4546
| `useCustomSortRecursively` | When `true`, will use the custom sort keywords recursively on a file, when using custom sort. | `false` |
4647
| `useLeadingDashes` | When `true`, sorted YAML files begin with leading dashes. | `true` |

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-yaml-sort",
33
"displayName": "YAML Sort",
44
"description": "This VS Code extension exposes the possibility to sort, format and validate yaml files.",
5-
"version": "6.5.18",
5+
"version": "6.6.0",
66
"engines": {
77
"vscode": "^1.49.0"
88
},

src/test/suite/util/sort-util.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,24 @@ suite("Test Sort - customSort()", () => {
3333
test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `data` should return -1", () => {
3434
equal(sort.customSort("kind", "data"), -1)
3535
})
36-
test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `kind` should return 0", () => {
36+
test("when `custom` is `1` and keywords are `['kind', 'kind']` and a is `kind` and b is `kind` should return 0", () => {
3737
equal(sort.customSort("kind", "kind"), 0)
3838
})
39+
40+
sort.custom = 0
41+
sort.settings.sortOrderReverse = false
42+
test("when `sortOrderReverse` is false and keywords are `['kind', 'data']` should return 1", () => {
43+
equal(sort.customSort("data", "kind"), 1)
44+
equal(sort.customSort("kind", "data"), -1)
45+
equal(sort.customSort("kind", "kind"), 0)
46+
})
47+
sort.settings.sortOrderReverse = true
48+
test("when `sortOrderReverse` is true and keywords are `['abc', 'bcd']` should return -1", () => {
49+
equal(sort.customSort("abc", "bcd"), -1)
50+
equal(sort.customSort("b", "a"), 1)
51+
equal(sort.customSort("a", "a"), 0)
52+
})
53+
sort.custom = 1
54+
sort.settings.sortOrderReverse = false
55+
3956
})

src/util/sort-util.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,27 @@ export class SortUtil {
1616
const indexB = sortOrder.indexOf(b)
1717

1818
if (indexA > -1 && indexB > -1) {
19-
return SortUtil.compare(sortOrderReverse, indexA, indexB)
19+
return SortUtil.compare(indexA, indexB)
2020
}
2121
if (indexA !== -1 && indexB === -1) {
2222
return -1
2323
}
2424
if (indexA === -1 && indexB !== -1) {
2525
return 1
2626
}
27+
if (sortOrderReverse) {
28+
return this.localeSort(b, a)
29+
}
2730
return this.localeSort(a, b)
2831
}
2932

30-
static compare(reverse: boolean, a: number, b: number) {
33+
static compare(a: number, b: number) {
3134
if (a > b) {
32-
return reverse ? -1 : +1
35+
return 1
3336
}
3437

3538
if (a < b) {
36-
return reverse ? +1 : -1
39+
return -1
3740
}
3841

3942
return 0

0 commit comments

Comments
 (0)