Skip to content

Commit 3d95996

Browse files
authored
Merge pull request #255 from microsoft/awful-ptarmigan
JSON sort: sort symbols first, then lower case, then upper case
2 parents 3da5591 + af43912 commit 3d95996

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/test/sort.test.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,11 +1449,11 @@ suite('Sort JSON', () => {
14491449

14501450
const expected = [
14511451
'{',
1452-
' "Test": "Test",',
1453-
' "tEst": "tEst",',
1454-
' "teSt": "teSt",',
1452+
' "test": "test",',
14551453
' "tesT": "tesT",',
1456-
' "test": "test"',
1454+
' "teSt": "teSt",',
1455+
' "tEst": "tEst",',
1456+
' "Test": "Test"',
14571457
'}'
14581458
].join('\n');
14591459

@@ -1463,21 +1463,41 @@ suite('Sort JSON', () => {
14631463
test('sorting an already sorted JSON object with mixed case keys', () => {
14641464
const content = [
14651465
'{',
1466-
' "Test": "Test",',
1467-
' "tEst": "tEst",',
1468-
' "teSt": "teSt",',
1466+
' "test": "test",',
14691467
' "tesT": "tesT",',
1470-
' "test": "test"',
1468+
' "teSt": "teSt",',
1469+
' "tEst": "tEst",',
1470+
' "Test": "Test"',
14711471
'}'
14721472
].join('\n');
14731473

14741474
const expected = [
14751475
'{',
1476-
' "Test": "Test",',
1477-
' "tEst": "tEst",',
1478-
' "teSt": "teSt",',
1476+
' "test": "test",',
14791477
' "tesT": "tesT",',
1480-
' "test": "test"',
1478+
' "teSt": "teSt",',
1479+
' "tEst": "tEst",',
1480+
' "Test": "Test"',
1481+
'}'
1482+
].join('\n');
1483+
1484+
testSort(content, expected, formattingOptions);
1485+
});
1486+
1487+
test('sorting symbols before letters', () => {
1488+
const content = [
1489+
'{',
1490+
' "Test": "Test",',
1491+
' "test": "test",',
1492+
' "[test]: "test',
1493+
'}'
1494+
].join('\n');
1495+
1496+
const expected = [
1497+
'{',
1498+
' "[test]: "test,',
1499+
' "test": "test",',
1500+
' "Test": "Test"',
14811501
'}'
14821502
].join('\n');
14831503

src/utils/sort.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,8 @@ function sortJsoncDocument(jsonDocument: TextDocument, propertyTree: PropertyTre
375375
return sortedJsonDocument;
376376
}
377377

378-
function sortPropertiesCaseSensitive(properties: PropertyTree[]): void {
379-
properties.sort((a, b) => {
380-
const aName = a.propertyName ?? '';
381-
const bName = b.propertyName ?? '';
382-
return aName < bName ? -1 : aName > bName ? 1 : 0;
383-
});
378+
function sortProperties(properties: PropertyTree[]): void {
379+
properties.sort((a, b) => a.propertyName.localeCompare(b.propertyName));
384380
}
385381

386382
function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningLineNumber: number) {
@@ -397,7 +393,7 @@ function updateSortingQueue(queue: any[], propertyTree: PropertyTree, beginningL
397393
const diff = minimumBeginningLineNumber - propertyTree.beginningLineNumber!;
398394
beginningLineNumber = beginningLineNumber + diff;
399395

400-
sortPropertiesCaseSensitive(propertyTree.childrenProperties);
396+
sortProperties(propertyTree.childrenProperties);
401397

402398
queue.push(new SortingRange(beginningLineNumber, propertyTree.childrenProperties));
403399

0 commit comments

Comments
 (0)