From d6c114c32ab0b1ceac7069e12f8d5d0fac2cfe97 Mon Sep 17 00:00:00 2001 From: Hai Cao Date: Mon, 11 Jul 2022 15:35:08 -0700 Subject: [PATCH] Fix connection sorting by name in OE (#19991) * use localeCompare & sort by title instead of name * fix tests --- .../connection/common/connectionConfig.ts | 34 ++----------------- .../test/common/connectionConfig.test.ts | 4 +-- 2 files changed, 5 insertions(+), 33 deletions(-) diff --git a/src/sql/platform/connection/common/connectionConfig.ts b/src/sql/platform/connection/common/connectionConfig.ts index 18b7ce7709..a3a60a25ee 100644 --- a/src/sql/platform/connection/common/connectionConfig.ts +++ b/src/sql/platform/connection/common/connectionConfig.ts @@ -48,6 +48,7 @@ export class ConnectionConfig { const config = this.configurationService.inspect(GROUPS_CONFIG_KEY); let { userValue } = config; const { workspaceValue } = config; + const sortBy = this.configurationService.getValue(CONNECTIONS_SORT_BY_CONFIG_KEY); if (userValue) { if (workspaceValue) { @@ -57,23 +58,8 @@ export class ConnectionConfig { allGroups = allGroups.concat(userValue); } - const sortBy = this.configurationService.getValue(CONNECTIONS_SORT_BY_CONFIG_KEY); - let sortFunc: (a: IConnectionProfileGroup, b: IConnectionProfileGroup) => number; - if (sortBy === ConnectionsSortOrder.displayName) { - sortFunc = ((a, b) => { - if (a.name < b.name) { - return -1; - } else if (a.name > b.name) { - return 1; - } else { - return 0; - } - }); - } - - if (sortFunc) { - allGroups.sort(sortFunc); + allGroups.sort((a, b) => a.name.localeCompare(b.name)); } return deepClone(allGroups).map(g => { @@ -249,24 +235,10 @@ export class ConnectionConfig { let connectionProfiles = this.getIConnectionProfileStores(getWorkspaceConnections).map(p => { return ConnectionProfile.createFromStoredProfile(p, this._capabilitiesService); }); - const sortBy = this.configurationService.getValue(CONNECTIONS_SORT_BY_CONFIG_KEY); - let sortFunc: (a: ConnectionProfile, b: ConnectionProfile) => number; if (sortBy === ConnectionsSortOrder.displayName) { - sortFunc = ((a, b) => { - if (a.title < b.title) { - return -1; - } else if (a.title > b.title) { - return 1; - } else { - return 0; - } - }); - } - - if (sortFunc) { - connectionProfiles.sort(sortFunc); + connectionProfiles.sort((a, b) => a.title.localeCompare(b.title)); } return connectionProfiles; diff --git a/src/sql/platform/connection/test/common/connectionConfig.test.ts b/src/sql/platform/connection/test/common/connectionConfig.test.ts index c16f3a784a..76a7a30964 100644 --- a/src/sql/platform/connection/test/common/connectionConfig.test.ts +++ b/src/sql/platform/connection/test/common/connectionConfig.test.ts @@ -250,7 +250,7 @@ suite('ConnectionConfig', () => { assert.strictEqual(allGroups.length, testGroups.length, 'did not meet the expected length'); assert.ok(groupsAreEqual(allGroups, testGroups), 'the groups returned did not match expectation'); - assert.ok(allGroups.slice(1).every((item, i) => allGroups[i].name <= item.name), 'the groups are not sorted correctly'); + assert.ok(allGroups.slice(1).every((item, i) => allGroups[i].name.localeCompare(item.name) <= 0), 'the groups are not sorted correctly'); }); test('getAllGroups should return groups sorted by date added given datasource.connectionsSortOrder is set to \'' + ConnectionsSortOrder.dateAdded + '\'', () => { @@ -420,7 +420,7 @@ suite('ConnectionConfig', () => { let config = new ConnectionConfig(configurationService, capabilitiesService.object); let allConnections = config.getConnections(true); assert.strictEqual(allConnections.length, testConnections.length, 'The result connections length is invalid'); - assert.ok(allConnections.slice(1).every((item, i) => allConnections[i].title <= item.title), 'The connections are not sorted correctly'); + assert.ok(allConnections.slice(1).every((item, i) => allConnections[i].title.localeCompare(item.title) <= 0), 'The connections are not sorted correctly'); }); test('getConnections should return connections sorted by date added given datasource.connectionsSortOrder is set to \'' + ConnectionsSortOrder.dateAdded + '\'', () => {