Skip to content

Commit

Permalink
Fixing sorting of directory and filenames with numbers (fixes #17495)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchadwick committed Jan 15, 2017
1 parent 3145a81 commit 2bb0cf9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
21 changes: 1 addition & 20 deletions src/vs/base/common/comparers.ts
Expand Up @@ -7,27 +7,8 @@
import scorer = require('vs/base/common/scorer');
import strings = require('vs/base/common/strings');

const FileNameMatch = /^([^.]*)(\.(.*))?$/;

export function compareFileNames(one: string, other: string): number {
let oneMatch = FileNameMatch.exec(one.toLowerCase());
let otherMatch = FileNameMatch.exec(other.toLowerCase());

let oneName = oneMatch[1] || '';
let oneExtension = oneMatch[3] || '';

let otherName = otherMatch[1] || '';
let otherExtension = otherMatch[3] || '';

if (oneName !== otherName) {
return oneName < otherName ? -1 : 1;
}

if (oneExtension === otherExtension) {
return 0;
}

return oneExtension < otherExtension ? -1 : 1;
return (one || '').localeCompare((other || ''), undefined, { numeric: true, sensitivity: 'base' });
}

export function compareAnything(one: string, other: string, lookFor: string): number {
Expand Down
10 changes: 8 additions & 2 deletions src/vs/base/test/common/comparers.test.ts
Expand Up @@ -12,10 +12,16 @@ suite('Comparers', () => {

test('compareFileNames', () => {

assert(compareFileNames(null, null) === 0, 'null should be equal');
assert(compareFileNames(null, 'abc') < 0, 'null should be come before real values');
assert(compareFileNames('', '') === 0, 'empty should be equal');
assert(compareFileNames('abc', 'abc') === 0, 'equal names should be equal');
assert(compareFileNames('.abc', '.abc') === 0, 'equal full names should be equal');
assert(compareFileNames('.env', '.env.example') < 0);
assert(compareFileNames('.env.example', '.gitattributes') < 0);
assert(compareFileNames('.env', '.env.example') < 0, 'filenames with extensions should come after those without');
assert(compareFileNames('.env.example', '.gitattributes') < 0, 'filenames starting with dots and with extensions should still sort properly');
assert(compareFileNames('1', '1') === 0, 'numerically equal full names should be equal');
assert(compareFileNames('abc1.txt', 'abc1.txt') === 0, 'equal filenames with numbers should be equal');
assert(compareFileNames('abc1.txt', 'abc2.txt') < 0, 'filenames with numbers should be in numerical order, not alphabetical order');
assert(compareFileNames('abc2.txt', 'abc10.txt') < 0, 'filenames with numbers should be in numerical order even when they are multiple digits long');
});
});
4 changes: 0 additions & 4 deletions src/vs/workbench/parts/files/browser/views/explorerViewer.ts
Expand Up @@ -652,10 +652,6 @@ export class FileSorter implements ISorter {
return 1;
}

if (statA.isDirectory && statB.isDirectory) {
return statA.name.toLowerCase().localeCompare(statB.name.toLowerCase());
}

if (statA instanceof NewStatPlaceholder) {
return -1;
}
Expand Down

0 comments on commit 2bb0cf9

Please sign in to comment.