Skip to content

Commit 93be9f2

Browse files
authored
Merge pull request #226 from id-aditya/master
Adds ability to sort in descending (reverse) order.
2 parents 554c020 + fe90e73 commit 93be9f2

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@
157157
"default": false,
158158
"description": "When `true`, will sort arrays"
159159
},
160+
"vscode-yaml-sort.sortOrderReverse": {
161+
"type": "boolean",
162+
"default": false,
163+
"description": "When `true`, will sort in reverse order"
164+
},
160165
"vscode-yaml-sort.sortOnSave": {
161166
"type": "number",
162167
"default": 0,

src/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class Settings {
5050
schema = this.getSchema()
5151
sortArrays = this.getBoolean("sortArrays")
5252
sortOnSave = this.getNumber("sortOnSave")
53+
sortOrderReverse = this.getBoolean("sortOrderReverse")
5354
useCustomSortRecursively = this.getBoolean("useCustomSortRecursively")
5455
useLeadingDashes = this.getBoolean("useLeadingDashes")
5556
useArrayProcessor = this.getBoolean("useArrayProcessor")

src/util/sort-util.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ export class SortUtil {
1010
}
1111

1212
customSort(a: string, b: string): number {
13+
const sortOrderReverse = this.settings.sortOrderReverse
1314
const sortOrder = this.settings.getCustomSortKeywords(this.custom)
1415
const indexA = sortOrder.indexOf(a)
1516
const indexB = sortOrder.indexOf(b)
1617

1718
if (indexA > -1 && indexB > -1) {
18-
return SortUtil.compare(indexA, indexB)
19+
return SortUtil.compare(sortOrderReverse, indexA, indexB)
1920
}
2021
if (indexA !== -1 && indexB === -1) {
2122
return -1
@@ -26,13 +27,15 @@ export class SortUtil {
2627
return this.localeSort(a, b)
2728
}
2829

29-
static compare(a: number, b: number) {
30+
static compare(reverse: boolean, a: number, b: number) {
3031
if (a > b) {
31-
return 1
32+
return reverse ? -1 : +1
3233
}
34+
3335
if (a < b) {
34-
return -1
36+
return reverse ? +1 : -1
3537
}
38+
3639
return 0
3740
}
3841

0 commit comments

Comments
 (0)