Skip to content

Commit de1dc41

Browse files
authored
feat: Add support for sorting problems by acceptance rate (#728)
1 parent e93bb6e commit de1dc41

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
136136
| `leetcode.showCommentDescription` | Specify whether to include the problem description in the comments | `false` |
137137
| `leetcode.useEndpointTranslation` | Use endpoint's translation (if available) | `true` |
138138
| `leetcode.colorizeProblems` | Add difficulty badge and colorize problems files in explorer tree | `true` |
139+
| `leetcode.problems.sortStrategy` | Specify sorting strategy for problems list | `None` |
139140

140141
## Want Help?
141142

package.json

+23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"onCommand:leetcode.testSolution",
3939
"onCommand:leetcode.submitSolution",
4040
"onCommand:leetcode.switchDefaultLanguage",
41+
"onCommand:leetcode.problems.sort",
4142
"onView:leetCodeExplorer"
4243
],
4344
"main": "./out/src/extension",
@@ -134,6 +135,12 @@
134135
"command": "leetcode.switchDefaultLanguage",
135136
"title": "Switch Default Language",
136137
"category": "LeetCode"
138+
},
139+
{
140+
"command": "leetcode.problems.sort",
141+
"title": "Sort Problems",
142+
"category": "LeetCode",
143+
"icon": "$(sort-precedence)"
137144
}
138145
],
139146
"viewsContainers": {
@@ -179,6 +186,11 @@
179186
"command": "leetcode.pickOne",
180187
"when": "view == leetCodeExplorer",
181188
"group": "overflow@0"
189+
},
190+
{
191+
"command": "leetcode.problems.sort",
192+
"when": "view == leetCodeExplorer",
193+
"group": "overflow@1"
182194
}
183195
],
184196
"view/item/context": [
@@ -677,6 +689,17 @@
677689
"default": true,
678690
"scope": "application",
679691
"description": "Add difficulty badge and colorize problems files in explorer tree."
692+
},
693+
"leetcode.problems.sortStrategy": {
694+
"type": "string",
695+
"default": "None",
696+
"scope": "application",
697+
"enum": [
698+
"None",
699+
"Acceptance Rate (Ascending)",
700+
"Acceptance Rate (Descending)"
701+
],
702+
"description": "Sorting strategy for problems list."
680703
}
681704
}
682705
}

src/commands/plugin.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider";
56
import { leetCodeExecutor } from "../leetCodeExecutor";
67
import { IQuickItemEx } from "../shared";
7-
import { Endpoint } from "../shared";
8+
import { Endpoint, SortingStrategy } from "../shared";
89
import { DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
910
import { deleteCache } from "./cache";
1011

@@ -52,3 +53,36 @@ export function getLeetCodeEndpoint(): string {
5253
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
5354
return leetCodeConfig.get<string>("endpoint", Endpoint.LeetCode);
5455
}
56+
57+
const SORT_ORDER: SortingStrategy[] = [
58+
SortingStrategy.None,
59+
SortingStrategy.AcceptanceRateAsc,
60+
SortingStrategy.AcceptanceRateDesc,
61+
];
62+
63+
export async function switchSortingStrategy(): Promise<void> {
64+
const currentStrategy: SortingStrategy = getSortingStrategy();
65+
const picks: Array<IQuickItemEx<string>> = [];
66+
picks.push(
67+
...SORT_ORDER.map((s: SortingStrategy) => {
68+
return {
69+
label: `${currentStrategy === s ? "$(check)" : " "} ${s}`,
70+
value: s,
71+
};
72+
}),
73+
);
74+
75+
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
76+
if (!choice || choice.value === currentStrategy) {
77+
return;
78+
}
79+
80+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
81+
await leetCodeConfig.update("problems.sortStrategy", choice.value, true);
82+
await leetCodeTreeDataProvider.refresh();
83+
}
84+
85+
export function getSortingStrategy(): SortingStrategy {
86+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
87+
return leetCodeConfig.get<SortingStrategy>("problems.sortStrategy", SortingStrategy.None);
88+
}

src/explorer/LeetCodeNode.ts

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ export class LeetCodeNode {
5555
};
5656
}
5757

58+
public get acceptanceRate(): number {
59+
return Number(this.passRate.slice(0, -1).trim());
60+
}
61+
5862
public get uri(): Uri {
5963
return Uri.from({
6064
scheme: "leetcode",

src/explorer/explorerNodeManager.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import * as _ from "lodash";
55
import { Disposable } from "vscode";
66
import * as list from "../commands/list";
7-
import { Category, defaultProblem, ProblemState } from "../shared";
7+
import { getSortingStrategy } from "../commands/plugin";
8+
import { Category, defaultProblem, ProblemState, SortingStrategy } from "../shared";
89
import { shouldHideSolvedProblem } from "../utils/settingUtils";
910
import { LeetCodeNode } from "./LeetCodeNode";
1011

@@ -56,7 +57,9 @@ class ExplorerNodeManager implements Disposable {
5657
}
5758

5859
public getAllNodes(): LeetCodeNode[] {
59-
return Array.from(this.explorerNodeMap.values());
60+
return this.applySortingStrategy(
61+
Array.from(this.explorerNodeMap.values()),
62+
);
6063
}
6164

6265
public getAllDifficultyNodes(): LeetCodeNode[] {
@@ -114,7 +117,7 @@ class ExplorerNodeManager implements Disposable {
114117
res.push(node);
115118
}
116119
}
117-
return res;
120+
return this.applySortingStrategy(res);
118121
}
119122

120123
public getChildrenNodesById(id: string): LeetCodeNode[] {
@@ -142,7 +145,7 @@ class ExplorerNodeManager implements Disposable {
142145
break;
143146
}
144147
}
145-
return res;
148+
return this.applySortingStrategy(res);
146149
}
147150

148151
public dispose(): void {
@@ -186,6 +189,15 @@ class ExplorerNodeManager implements Disposable {
186189
break;
187190
}
188191
}
192+
193+
private applySortingStrategy(nodes: LeetCodeNode[]): LeetCodeNode[] {
194+
const strategy: SortingStrategy = getSortingStrategy();
195+
switch (strategy) {
196+
case SortingStrategy.AcceptanceRateAsc: return nodes.sort((x: LeetCodeNode, y: LeetCodeNode) => Number(x.acceptanceRate) - Number(y.acceptanceRate));
197+
case SortingStrategy.AcceptanceRateDesc: return nodes.sort((x: LeetCodeNode, y: LeetCodeNode) => Number(y.acceptanceRate) - Number(x.acceptanceRate));
198+
default: return nodes;
199+
}
200+
}
189201
}
190202

191203
export const explorerNodeManager: ExplorerNodeManager = new ExplorerNodeManager();

src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
6666
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
6767
vscode.commands.registerCommand("leetcode.addFavorite", (node: LeetCodeNode) => star.addFavorite(node)),
6868
vscode.commands.registerCommand("leetcode.removeFavorite", (node: LeetCodeNode) => star.removeFavorite(node)),
69+
vscode.commands.registerCommand("leetcode.problems.sort", () => plugin.switchSortingStrategy()),
6970
);
7071

7172
await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());

src/shared.ts

+8
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ export enum DescriptionConfiguration {
116116
}
117117

118118
export const leetcodeHasInited: string = "leetcode.hasInited";
119+
120+
export enum SortingStrategy {
121+
None = "None",
122+
AcceptanceRateAsc = "Acceptance Rate (Ascending)",
123+
AcceptanceRateDesc = "Acceptance Rate (Descending)",
124+
FrequencyAsc = "Frequency (Ascending)",
125+
FrequencyDesc = "Frequency (Descending)",
126+
}

0 commit comments

Comments
 (0)