Skip to content

Commit 0dae879

Browse files
committed
add ability to sort by id, title, and score
1 parent 8042bcc commit 0dae879

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/popup/company.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ a {
4848

4949
a:hover {
5050
color: lightcyan;
51+
}
52+
53+
.header {
54+
color: lightgreen;
55+
56+
}
57+
58+
.header:hover {
59+
cursor: pointer;
60+
color: lightcyan;
61+
background-color: #202020;
5162
}

src/popup/company.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ <h1 id="title">Amazon</h1>
1616
</p>
1717
<table id="solutionTable">
1818
<tr>
19-
<th>#</th>
20-
<th>Title</th>
21-
<th>Score</th>
19+
<th id="#" class="header">#</th>
20+
<th id="Title" class="header">Title</th>
21+
<th id="Score" class="header">Score</th>
2222
</tr>
2323
</table>
2424
<script src="../../dist/popup/company.js"></script>

src/popup/company.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
// define solutions and companyName outside of the functions so they can be accessed globally
2+
let solutions = [];
3+
let companyName = "Amazon";
4+
15
function main() {
26

3-
let companyName = "Amazon";
4-
let solutions = [];
57

68
chrome.storage.local.get("clickedCompany", function (data) {
79
companyName = data.clickedCompany;
810
document.getElementById("title").textContent = companyName;
911
document.title = companyName + "'s favorite problems"
1012
});
1113

14+
addCompanyProblems("Score");
15+
16+
// attach click listeners to table headers for sorting
17+
document.getElementById('#').addEventListener('click', () => sortBy('#'));
18+
document.getElementById('Title').addEventListener('click', () => sortBy('Title'));
19+
document.getElementById('Score').addEventListener('click', () => sortBy('Score'));
20+
}
21+
22+
// Adds the company problems by sorting method
23+
function addCompanyProblems(sortMethod) {
1224
chrome.storage.local.get("leetcodeProblems", function (data) {
1325
data.leetcodeProblems.questions.forEach(question => {
1426
if (!question.companies) return;
@@ -26,7 +38,9 @@ function main() {
2638

2739
const table = document.getElementById("solutionTable");
2840

29-
solutions.sort((a, b) => b.score - a.score);
41+
if (sortMethod === "Score") {
42+
solutions.sort((a, b) => b.score - a.score);
43+
}
3044

3145
solutions.forEach(solution => {
3246
const row = table.insertRow(-1);
@@ -38,5 +52,34 @@ function main() {
3852
});
3953
}
4054

55+
function sortBy(column) {
56+
if (column === 'Score') {
57+
solutions.sort((a, b) => b.score - a.score);
58+
}
59+
else if (column === 'Title') {
60+
solutions.sort((a, b) => a.title.localeCompare(b.title));
61+
}
62+
else {
63+
solutions.sort((a, b) => a.id - b.id);
64+
}
65+
66+
// after sorting, you might want to re-render your table
67+
const table = document.getElementById("solutionTable");
68+
69+
// remove all existing rows
70+
while (table.rows.length > 1) {
71+
table.deleteRow(1);
72+
}
73+
74+
// add sorted rows
75+
solutions.forEach(solution => {
76+
const row = table.insertRow(-1);
77+
row.insertCell(0).innerText = solution.id;
78+
const titleCell = row.insertCell(1);
79+
titleCell.innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`;
80+
row.insertCell(2).innerText = solution.score;
81+
});
82+
}
83+
4184
/* Run the script */
42-
main();
85+
main();

0 commit comments

Comments
 (0)