diff --git a/src/test/sort.test.ts b/src/test/sort.test.ts index 6c99016..95f2a00 100644 --- a/src/test/sort.test.ts +++ b/src/test/sort.test.ts @@ -1435,4 +1435,52 @@ suite('Sort JSON', () => { testSort(content, expected, formattingOptions); }); + + test('sorting a JSON object with mixed case keys', () => { + const content = [ + '{', + ' "tEst": "tEst",', + ' "tesT": "tesT",', + ' "teSt": "teSt",', + ' "Test": "Test",', + ' "test": "test"', + '}' + ].join('\n'); + + const expected = [ + '{', + ' "Test": "Test",', + ' "tEst": "tEst",', + ' "teSt": "teSt",', + ' "tesT": "tesT",', + ' "test": "test"', + '}' + ].join('\n'); + + testSort(content, expected, formattingOptions); + }); + + test('sorting an already sorted JSON object with mixed case keys', () => { + const content = [ + '{', + ' "Test": "Test",', + ' "tEst": "tEst",', + ' "teSt": "teSt",', + ' "tesT": "tesT",', + ' "test": "test"', + '}' + ].join('\n'); + + const expected = [ + '{', + ' "Test": "Test",', + ' "tEst": "tEst",', + ' "teSt": "teSt",', + ' "tesT": "tesT",', + ' "test": "test"', + '}' + ].join('\n'); + + testSort(content, expected, formattingOptions); + }); }); diff --git a/src/utils/sort.ts b/src/utils/sort.ts index 5beeb4e..b04b644 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -375,6 +375,14 @@ 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 updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningLineNumber: number) { if (propertyTree.childrenProperties.length === 0) { return; @@ -389,6 +397,8 @@ function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningL const diff = minimumBeginningLineNumber - propertyTree.beginningLineNumber!; beginningLineNumber = beginningLineNumber + diff; + sortPropertiesCaseSensitive(propertyTree.childrenProperties); + queue.push(new SortingRange(beginningLineNumber, propertyTree.childrenProperties)); } else if (propertyTree.type === Container.Array) {