1+ // define solutions and companyName outside of the functions so they can be accessed globally
2+ let solutions = [ ] ;
3+ let companyName = "Amazon" ;
4+
15function 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