Skip to content

Commit

Permalink
Support sorting in reverse order (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalre committed Oct 28, 2024
1 parent 93be9f2 commit 7302146
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
# 6.6.0 - Oct 28, 2024
✨ Feature
* (refs [#225](https://github.com/pascalre/vscode-yaml-sort/issues/225)) Support sorting in reverse order

# 6.5.18 - Oct 28, 2024
📦 Dependencies
* Update dependencies to latest versions
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This extension contributes the following settings:
| `schema` | Schema to use. Possible values are `HOMEASSISTANT_SCHEMA`, `CLOUDFORMATION_SCHEMA`, `CORE_SCHEMA`, `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`. | `DEFAULT_SCHEMA` |
| `sortArrays` | When `true`, will sort arrays | `false` |
| `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` |
| `sortOrderReverse` | When `true`, will sort in reverse order | `false` |
| `useAsFormatter` | When `true`, will enable default YAML formatter (requires restart). | `false` |
| `useCustomSortRecursively` | When `true`, will use the custom sort keywords recursively on a file, when using custom sort. | `false` |
| `useLeadingDashes` | When `true`, sorted YAML files begin with leading dashes. | `true` |
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-yaml-sort",
"displayName": "YAML Sort",
"description": "This VS Code extension exposes the possibility to sort, format and validate yaml files.",
"version": "6.5.18",
"version": "6.6.0",
"engines": {
"vscode": "^1.49.0"
},
Expand Down
19 changes: 18 additions & 1 deletion src/test/suite/util/sort-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,24 @@ suite("Test Sort - customSort()", () => {
test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `data` should return -1", () => {
equal(sort.customSort("kind", "data"), -1)
})
test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `kind` and b is `kind` should return 0", () => {
test("when `custom` is `1` and keywords are `['kind', 'kind']` and a is `kind` and b is `kind` should return 0", () => {
equal(sort.customSort("kind", "kind"), 0)
})

sort.custom = 0
sort.settings.sortOrderReverse = false
test("when `sortOrderReverse` is false and keywords are `['kind', 'data']` should return 1", () => {
equal(sort.customSort("data", "kind"), 1)
equal(sort.customSort("kind", "data"), -1)
equal(sort.customSort("kind", "kind"), 0)
})
sort.settings.sortOrderReverse = true
test("when `sortOrderReverse` is true and keywords are `['abc', 'bcd']` should return -1", () => {
equal(sort.customSort("abc", "bcd"), -1)
equal(sort.customSort("b", "a"), 1)
equal(sort.customSort("a", "a"), 0)
})
sort.custom = 1
sort.settings.sortOrderReverse = false

})
12 changes: 8 additions & 4 deletions src/util/sort-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export class SortUtil {
const indexB = sortOrder.indexOf(b)

if (indexA > -1 && indexB > -1) {
return SortUtil.compare(sortOrderReverse, indexA, indexB)
if (sortOrderReverse) {
return SortUtil.compare(indexB, indexA)
} else {
return SortUtil.compare(indexA, indexB)
}
}
if (indexA !== -1 && indexB === -1) {
return -1
Expand All @@ -27,13 +31,13 @@ export class SortUtil {
return this.localeSort(a, b)
}

static compare(reverse: boolean, a: number, b: number) {
static compare(a: number, b: number) {
if (a > b) {
return reverse ? -1 : +1
return 1
}

if (a < b) {
return reverse ? +1 : -1
return -1
}

return 0
Expand Down

0 comments on commit 7302146

Please sign in to comment.