From d5661b0be443416ca40e79c475526a3e82d1aa4c Mon Sep 17 00:00:00 2001 From: angelozerr Date: Mon, 11 Jan 2021 18:30:18 +0100 Subject: [PATCH] Elements should be sorted alphabetically in the explorer Fixes #63 Signed-off-by: azerr --- src/explorer/models/consumerGroups.ts | 21 ++++++++++++++++++--- src/explorer/models/kafka.ts | 13 ++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/explorer/models/consumerGroups.ts b/src/explorer/models/consumerGroups.ts index 3b6a44b..20c0501 100644 --- a/src/explorer/models/consumerGroups.ts +++ b/src/explorer/models/consumerGroups.ts @@ -14,12 +14,20 @@ export class ConsumerGroupsItem extends NodeBase { super(parent); } - async computeChildren() : Promise { + async computeChildren(): Promise { const client = this.getParent().client; - const consumerGroupIds = await client.getConsumerGroupIds(); + const consumerGroupIds = (await client.getConsumerGroupIds()) + .sort(this.sortByAscending); return Promise.resolve( consumerGroupIds.map((consumerGroupId) => (new ConsumerGroupItem(consumerGroupId, this)))); } + + private sortByAscending(a: string, b: string): -1 | 0 | 1 { + if (a.toLowerCase() < b.toLowerCase()) { return -1; } + if (a.toLowerCase() > b.toLowerCase()) { return 1; } + return 0; + } + getParent(): ClusterItem { return super.getParent(); } @@ -38,12 +46,19 @@ class ConsumerGroupItem extends NodeBase { async computeChildren(): Promise { const client = this.getParent().getParent().client; const groupDetails = await client.getConsumerGroupDetails(this.consumerGroupId); + const members = groupDetails.members.sort(this.sortByMemberIdAscending); return [ new ConsumerGroupDetailsItem("State", groupDetails.state, this), - new ConsumerGroupMembersItem(groupDetails.members, this), + new ConsumerGroupMembersItem(members, this), ]; } + private sortByMemberIdAscending(a: ConsumerGroupMember, b: ConsumerGroupMember): -1 | 0 | 1 { + if (a.memberId.toLowerCase() < b.memberId.toLowerCase()) { return -1; } + if (a.memberId.toLowerCase() > b.memberId.toLowerCase()) { return 1; } + return 0; + } + getParent(): ConsumerGroupsItem { return super.getParent(); } diff --git a/src/explorer/models/kafka.ts b/src/explorer/models/kafka.ts index 2344d96..4d6cc45 100644 --- a/src/explorer/models/kafka.ts +++ b/src/explorer/models/kafka.ts @@ -1,5 +1,5 @@ import { Disposable, TreeItemCollapsibleState } from "vscode"; -import { ClientAccessor } from "../../client"; +import { ClientAccessor, Cluster } from "../../client"; import { ClusterSettings } from "../../settings"; import { ClusterItem, NoClusterItem } from "./cluster"; import { NodeBase } from "./nodeBase"; @@ -16,7 +16,8 @@ export class KafkaModel extends NodeBase implements Disposable { } public async computeChildren(): Promise { - const clusters = this.clusterSettings.getAll(); + const clusters = this.clusterSettings.getAll() + .sort(this.sortByNameAscending); if (clusters.length === 0) { return [new NoClusterItem(this)]; } @@ -25,6 +26,12 @@ export class KafkaModel extends NodeBase implements Disposable { }); } + private sortByNameAscending(a: Cluster, b: Cluster): -1 | 0 | 1 { + if (a.name.toLowerCase() < b.name.toLowerCase()) { return -1; } + if (a.name.toLowerCase() > b.name.toLowerCase()) { return 1; } + return 0; + } + public dispose(): void { this.children?.forEach(child => (child).dispose()); } @@ -33,6 +40,6 @@ export class KafkaModel extends NodeBase implements Disposable { return this.getChildren() .then(clusters => clusters.find(child => (child).cluster.name === clusterName) - ); + ); } }