From 5bada0b1dc21fd9c5752597dd1fd81b2074552e2 Mon Sep 17 00:00:00 2001 From: Aiday Marlen Kyzy Date: Tue, 10 Dec 2024 12:58:53 +0100 Subject: [PATCH 1/2] sorting symbols first, then lower case, then upper case --- src/test/sort.test.ts | 44 +++++++++++++++++++++++++++++++------------ src/utils/sort.ts | 41 ++++++++++++++++++++++++++++++++++------ 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/test/sort.test.ts b/src/test/sort.test.ts index 95f2a00..fc975ad 100644 --- a/src/test/sort.test.ts +++ b/src/test/sort.test.ts @@ -1449,11 +1449,11 @@ suite('Sort JSON', () => { const expected = [ '{', - ' "Test": "Test",', - ' "tEst": "tEst",', - ' "teSt": "teSt",', + ' "test": "test",', ' "tesT": "tesT",', - ' "test": "test"', + ' "teSt": "teSt",', + ' "tEst": "tEst",', + ' "Test": "Test"', '}' ].join('\n'); @@ -1463,21 +1463,41 @@ suite('Sort JSON', () => { test('sorting an already sorted JSON object with mixed case keys', () => { const content = [ '{', - ' "Test": "Test",', - ' "tEst": "tEst",', - ' "teSt": "teSt",', + ' "test": "test",', ' "tesT": "tesT",', - ' "test": "test"', + ' "teSt": "teSt",', + ' "tEst": "tEst",', + ' "Test": "Test"', '}' ].join('\n'); const expected = [ '{', - ' "Test": "Test",', - ' "tEst": "tEst",', - ' "teSt": "teSt",', + ' "test": "test",', ' "tesT": "tesT",', - ' "test": "test"', + ' "teSt": "teSt",', + ' "tEst": "tEst",', + ' "Test": "Test"', + '}' + ].join('\n'); + + testSort(content, expected, formattingOptions); + }); + + test('sorting symbols before letters', () => { + const content = [ + '{', + ' "Test": "Test",', + ' "test": "test",', + ' "[test]: "test', + '}' + ].join('\n'); + + const expected = [ + '{', + ' "[test]: "test,', + ' "test": "test",', + ' "Test": "Test"', '}' ].join('\n'); diff --git a/src/utils/sort.ts b/src/utils/sort.ts index b04b644..eee161e 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -375,11 +375,40 @@ function sortJsoncDocument(jsonDocument: TextDocument, propertyTree: PropertyTre return sortedJsonDocument; } -function sortPropertiesCaseSensitive(properties: PropertyTree[]): void { - properties.sort((a, b) => { - const aName = a.propertyName ?? ''; - const bName = b.propertyName ?? ''; - return aName < bName ? -1 : aName > bName ? 1 : 0; +function sortProperties(properties: PropertyTree[]): void { + properties.sort((property1, property2) => { + const propertyName1 = property1.propertyName; + const propertyName2 = property2.propertyName; + let i = 0; + while (i < propertyName1.length && i < propertyName2.length) { + const chr1 = propertyName1[i]; + const ch2 = propertyName2[i]; + // If both characters are symbols, sort them normally + if (!/[a-zA-Z]/.test(chr1) && !/[a-zA-Z]/.test(ch2)) { + if (chr1 < ch2) return -1; + if (chr1 > ch2) return 1; + } + // If one is a symbol and the other is a letter, the symbol comes first + if (!/[a-zA-Z]/.test(chr1)) { + return -1; + } + if (!/[a-zA-Z]/.test(ch2)) { + return 1; + } + // If both are letters, lowercase comes before uppercase + if (chr1 === chr1.toLowerCase() && ch2 === ch2.toUpperCase()) { + return -1; + } + if (chr1 === chr1.toUpperCase() && ch2 === ch2.toLowerCase()) { + return 1; + } + // If both are letters and of the same case, compare them alphabetically + if (chr1 < ch2) { return -1 }; + if (chr1 > ch2) { return 1 }; + i++; + } + // If one string is shorter, the shorter string is considered lower + return propertyName1.length - propertyName2.length; }); } @@ -397,7 +426,7 @@ function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningL const diff = minimumBeginningLineNumber - propertyTree.beginningLineNumber!; beginningLineNumber = beginningLineNumber + diff; - sortPropertiesCaseSensitive(propertyTree.childrenProperties); + sortProperties(propertyTree.childrenProperties); queue.push(new SortingRange(beginningLineNumber, propertyTree.childrenProperties)); From af439123bf2a267fadb5e8701f259b0982d045fa Mon Sep 17 00:00:00 2001 From: Aiday Marlen Kyzy Date: Wed, 11 Dec 2024 09:21:42 +0100 Subject: [PATCH 2/2] using localecompare instead --- src/utils/sort.ts | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/src/utils/sort.ts b/src/utils/sort.ts index eee161e..2f3d539 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -376,40 +376,7 @@ function sortJsoncDocument(jsonDocument: TextDocument, propertyTree: PropertyTre } function sortProperties(properties: PropertyTree[]): void { - properties.sort((property1, property2) => { - const propertyName1 = property1.propertyName; - const propertyName2 = property2.propertyName; - let i = 0; - while (i < propertyName1.length && i < propertyName2.length) { - const chr1 = propertyName1[i]; - const ch2 = propertyName2[i]; - // If both characters are symbols, sort them normally - if (!/[a-zA-Z]/.test(chr1) && !/[a-zA-Z]/.test(ch2)) { - if (chr1 < ch2) return -1; - if (chr1 > ch2) return 1; - } - // If one is a symbol and the other is a letter, the symbol comes first - if (!/[a-zA-Z]/.test(chr1)) { - return -1; - } - if (!/[a-zA-Z]/.test(ch2)) { - return 1; - } - // If both are letters, lowercase comes before uppercase - if (chr1 === chr1.toLowerCase() && ch2 === ch2.toUpperCase()) { - return -1; - } - if (chr1 === chr1.toUpperCase() && ch2 === ch2.toLowerCase()) { - return 1; - } - // If both are letters and of the same case, compare them alphabetically - if (chr1 < ch2) { return -1 }; - if (chr1 > ch2) { return 1 }; - i++; - } - // If one string is shorter, the shorter string is considered lower - return propertyName1.length - propertyName2.length; - }); + properties.sort((a, b) => a.propertyName.localeCompare(b.propertyName)); } function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningLineNumber: number) {