Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ds/sort): Fallback to sorting by name when values are equal #2538

Merged
merged 7 commits into from
Nov 1, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import * as fs from "fs";
import * as zowe from "@zowe/cli";
import { DatasetTree } from "../../../src/dataset/DatasetTree";
import { ZoweDatasetNode } from "../../../src/dataset/ZoweDatasetNode";
import { DatasetFilterOpts, Gui, IZoweDatasetTreeNode, ProfilesCache, ValidProfileEnum } from "@zowe/zowe-explorer-api";
import {
DatasetFilterOpts,
DatasetSortOpts,
Gui,
IZoweDatasetTreeNode,
ProfilesCache,
SortDirection,
ValidProfileEnum,
} from "@zowe/zowe-explorer-api";
import { ZoweExplorerApiRegister } from "../../../src/ZoweExplorerApiRegister";
import { Profiles } from "../../../src/Profiles";
import * as utils from "../../../src/utils/ProfilesUtils";
Expand Down Expand Up @@ -2716,6 +2724,10 @@ describe("Dataset Tree Unit Tests - Sorting and Filtering operations", () => {
const nodeC = new ZoweDatasetNode("C", vscode.TreeItemCollapsibleState.Collapsed, pds, createISession());
nodeC.stats = { user: "someUser", modifiedDate: new Date("2022-03-15T16:30:00") };
pds.children = [nodeA, nodeB, nodeC];
pds.sort = {
method: DatasetSortOpts.Name,
direction: SortDirection.Ascending,
};
session.children = [pds];

return {
Expand Down Expand Up @@ -2785,6 +2797,22 @@ describe("Dataset Tree Unit Tests - Sorting and Filtering operations", () => {
expect(nodes.pds.children?.map((c: IZoweDatasetTreeNode) => c.label)).toStrictEqual(["B", "C", "A"]);
});

it("sorts by last modified date: handling 2 nodes with same date", async () => {
const mocks = getBlockMocks();
const nodes = nodesForSuite();
mocks.showQuickPick.mockResolvedValueOnce({ label: "$(fold) Sort Direction" });
mocks.showQuickPick.mockResolvedValueOnce({ label: "Descending" });
mocks.showQuickPick.mockResolvedValueOnce({ label: "$(calendar) Date Modified" });
// insert node with same date modified
const nodeD = new ZoweDatasetNode("D", vscode.TreeItemCollapsibleState.Collapsed, nodes.pds, createISession());
nodeD.stats = { user: "someUser", modifiedDate: new Date("2022-03-15T16:30:00") };
nodes.pds.children = [...(nodes.pds.children ?? []), nodeD];
await tree.sortPdsMembersDialog(nodes.pds);
expect(mocks.nodeDataChanged).toHaveBeenCalled();
expect(mocks.refreshElement).not.toHaveBeenCalled();
expect(nodes.pds.children?.map((c: IZoweDatasetTreeNode) => c.label)).toStrictEqual(["A", "D", "C", "B"]);
});

it("sorts by user ID", async () => {
const mocks = getBlockMocks();
const nodes = nodesForSuite();
Expand Down
40 changes: 28 additions & 12 deletions packages/zowe-explorer/src/dataset/ZoweDatasetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
*
* @returns {Promise<ZoweDatasetNode[]>}
*/
public async getChildren(): Promise<ZoweDatasetNode[]> {

Check warning on line 126 in packages/zowe-explorer/src/dataset/ZoweDatasetNode.ts

View workflow job for this annotation

GitHub Actions / lint

Async method 'getChildren' has a complexity of 38. Maximum allowed is 15
ZoweLogger.trace("ZoweDatasetNode.getChildren called.");
if (!this.pattern && contextually.isSessionNotFav(this)) {
return [
Expand Down Expand Up @@ -332,22 +332,38 @@
const sortLessThan = sort.direction == SortDirection.Ascending ? -1 : 1;
const sortGreaterThan = sortLessThan * -1;

const sortByName = (nodeA: IZoweDatasetTreeNode, nodeB: IZoweDatasetTreeNode): number =>
(nodeA.label as string) < (nodeB.label as string) ? sortLessThan : sortGreaterThan;

if (!a.stats && !b.stats) {
return (a.label as string) < (b.label as string) ? sortLessThan : sortGreaterThan;
return sortByName(a, b);
}

switch (sort.method) {
case DatasetSortOpts.LastModified:
a.description = dayjs(a.stats?.modifiedDate).format("YYYY/MM/DD HH:mm:ss");
b.description = dayjs(b.stats?.modifiedDate).format("YYYY/MM/DD HH:mm:ss");
return a.stats?.modifiedDate < b.stats?.modifiedDate ? sortLessThan : sortGreaterThan;
case DatasetSortOpts.UserId:
a.description = a.stats?.user;
b.description = b.stats?.user;
return a.stats?.user < b.stats?.user ? sortLessThan : sortGreaterThan;
case DatasetSortOpts.Name:
return (a.label as string) < (b.label as string) ? sortLessThan : sortGreaterThan;
if (sort.method === DatasetSortOpts.LastModified) {
const dateA = dayjs(a.stats?.modifiedDate);
const dateB = dayjs(b.stats?.modifiedDate);

a.description = dateA.format("YYYY/MM/DD HH:mm:ss");
b.description = dateB.format("YYYY/MM/DD HH:mm:ss");

// for dates that are equal down to the second, fallback to sorting by name
if (dateA.isSame(dateB, "second")) {
return sortByName(a, b);
}

return dateA.isBefore(dateB, "second") ? sortLessThan : sortGreaterThan;
} else if (sort.method === DatasetSortOpts.UserId) {
a.description = a.stats?.user;
b.description = b.stats?.user;

if (a.stats?.user === b.stats?.user) {
return sortByName(a, b);
}

return a.stats?.user < b.stats?.user ? sortLessThan : sortGreaterThan;
}

return sortByName(a, b);
};
}

Expand Down
Loading