Skip to content

Commit 5fb7d2a

Browse files
committed
Compare full filenames
This change fixes issue microsoft#99955 When comparing file and folder names, after grouping by type if applicable, compare the full name all at once instead of separately comparing the name and the extension. This change makes filename comparisons work the same as they do in the Mac File Explorer and the same as a standard locale-based sort with the numeric option (a form of natural sort) enabled. Windows File Explorer has a different sort order due to treating the dot character differently. Many terminal programs and sites like github also differ, using a unicode-sort instead of a locale-based sort.
1 parent 4e18099 commit 5fb7d2a

3 files changed

Lines changed: 116 additions & 147 deletions

File tree

‎src/vs/base/common/comparers.ts‎

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ const intlFileNameCollatorNumericCaseInsenstive: IdleValue<{ collator: Intl.Coll
3333
return {
3434
collator: collator
3535
};
36-
});
37-
36+
});/** Compares filenames without distinguishing the name from the extension. Disambiguates by unicode comparison. */
3837
export function compareFileNames(one: string | null, other: string | null, caseSensitive = false): number {
3938
const a = one || '';
4039
const b = other || '';
@@ -49,36 +48,16 @@ export function compareFileNames(one: string | null, other: string | null, caseS
4948
return result;
5049
}
5150

52-
/** Compares filenames by name then extension, sorting numbers numerically instead of alphabetically. */
53-
export function compareFileNamesNumeric(one: string | null, other: string | null): number {
54-
const [oneName, oneExtension] = extractNameAndExtension(one, true);
55-
const [otherName, otherExtension] = extractNameAndExtension(other, true);
51+
/** Compares filenames without distinguishing the name from the extension. Disambiguates by length, not unicode comparison. */
52+
export function compareFileNamesDefault(one: string | null, other: string | null): number {
5653
const collatorNumeric = intlFileNameCollatorNumeric.value.collator;
57-
const collatorNumericCaseInsensitive = intlFileNameCollatorNumericCaseInsenstive.value.collator;
58-
let result;
59-
60-
// Check for name differences, comparing numbers numerically instead of alphabetically.
61-
result = compareAndDisambiguateByLength(collatorNumeric, oneName, otherName);
62-
if (result !== 0) {
63-
return result;
64-
}
54+
one = one || '';
55+
other = other || '';
6556

66-
// Check for case insensitive extension differences, comparing numbers numerically instead of alphabetically.
67-
result = compareAndDisambiguateByLength(collatorNumericCaseInsensitive, oneExtension, otherExtension);
68-
if (result !== 0) {
69-
return result;
70-
}
71-
72-
// Disambiguate the extension case if needed.
73-
if (oneExtension !== otherExtension) {
74-
return collatorNumeric.compare(oneExtension, otherExtension);
75-
}
76-
77-
return 0;
57+
// Compare the entire filename - both name and extension - and disambiguate by length if needed
58+
return compareAndDisambiguateByLength(collatorNumeric, one, other);
7859
}
7960

80-
const FileNameMatch = /^(.*?)(\.([^.]*))?$/;
81-
8261
export function noIntlCompareFileNames(one: string | null, other: string | null, caseSensitive = false): number {
8362
if (!caseSensitive) {
8463
one = one && one.toLowerCase();
@@ -123,10 +102,12 @@ export function compareFileExtensions(one: string | null, other: string | null):
123102
return result;
124103
}
125104

126-
/** Compares filenames by extenson, then by name. Sorts numbers numerically, not alphabetically. */
127-
export function compareFileExtensionsNumeric(one: string | null, other: string | null): number {
128-
const [oneName, oneExtension] = extractNameAndExtension(one, true);
129-
const [otherName, otherExtension] = extractNameAndExtension(other, true);
105+
/** Compares filenames by extenson, then by full filename */
106+
export function compareFileExtensionsDefault(one: string | null, other: string | null): number {
107+
one = one || '';
108+
other = other || '';
109+
const oneExtension = extractExtension(one);
110+
const otherExtension = extractExtension(other);
130111
const collatorNumeric = intlFileNameCollatorNumeric.value.collator;
131112
const collatorNumericCaseInsensitive = intlFileNameCollatorNumericCaseInsenstive.value.collator;
132113
let result;
@@ -137,20 +118,12 @@ export function compareFileExtensionsNumeric(one: string | null, other: string |
137118
return result;
138119
}
139120

140-
// Compare names.
141-
result = compareAndDisambiguateByLength(collatorNumeric, oneName, otherName);
142-
if (result !== 0) {
143-
return result;
144-
}
145-
146-
// Disambiguate extension case if needed.
147-
if (oneExtension !== otherExtension) {
148-
return collatorNumeric.compare(oneExtension, otherExtension);
149-
}
150-
151-
return 0;
121+
// Compare full filenames
122+
return compareAndDisambiguateByLength(collatorNumeric, one, other);
152123
}
153124

125+
const FileNameMatch = /^(.*?)(\.([^.]*))?$/;
126+
154127
/** Extracts the name and extension from a full filename, with optional special handling for dotfiles */
155128
function extractNameAndExtension(str?: string | null, dotfilesAsNames = false): [string, string] {
156129
const match = str ? FileNameMatch.exec(str) as Array<string> : ([] as Array<string>);
@@ -166,6 +139,13 @@ function extractNameAndExtension(str?: string | null, dotfilesAsNames = false):
166139
return result;
167140
}
168141

142+
/** Extracts the extension from a full filename. Treats dotfiles as names, not extensions. */
143+
function extractExtension(str?: string | null): string {
144+
const match = str ? FileNameMatch.exec(str) as Array<string> : ([] as Array<string>);
145+
146+
return (match && match[1] && match[1].charAt(0) !== '.' && match[3]) || '';
147+
}
148+
169149
function compareAndDisambiguateByLength(collator: Intl.Collator, one: string, other: string) {
170150
// Check for differences
171151
let result = collator.compare(one, other);

0 commit comments

Comments
 (0)