From 7c6a1288e6ac0fb836d90bc61987ae5b876e714f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lukas=20Mau=C3=9F?= <lukas.mauss@mni.thm.de>
Date: Sat, 20 Apr 2019 13:29:47 +0200
Subject: [PATCH] Fix sorting

---
 .../comment-list/comment-list.component.html  | 63 ++++++++++---------
 .../comment-list/comment-list.component.ts    | 15 ++---
 2 files changed, 37 insertions(+), 41 deletions(-)

diff --git a/src/app/components/shared/comment-list/comment-list.component.html b/src/app/components/shared/comment-list/comment-list.component.html
index 15a8b680e..be6b01827 100644
--- a/src/app/components/shared/comment-list/comment-list.component.html
+++ b/src/app/components/shared/comment-list/comment-list.component.html
@@ -7,7 +7,7 @@
   <button mat-button *ngIf="searchBox.value || isIconHide" (click)="hideCommentsList=false; searchBox.value=''; isIconHide=false">
     <mat-icon>close</mat-icon>
   </button>
-  <button mat-button *ngIf="!searchBox.value && userRole == '1' && comments.length > 0"
+  <button mat-button *ngIf="!searchBox.value && userRole === '1' && comments.length > 0"
     [matTooltip]="'Export comments'" (click)="export(true)">
     <mat-icon class="add-icon" id="export-icon">cloud_download</mat-icon>
   </button>
@@ -16,39 +16,42 @@
     <mat-icon class="add-icon">add_circle</mat-icon>
   </button>
 
-  <mat-icon *ngIf="!searchBox.value" class="add-filterIcon" color="accent" mat-button [mat-menu-trigger-for]="menu">filter_list</mat-icon>
-    <mat-menu #menu="matMenu" xPosition="before">
-      <div id="filterIcon">
-        <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterMarkAsCorrect(); isIconHide=true" value="over">
-          <mat-icon>check_circle</mat-icon>
-        </button>
-
-        <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterFavorite(); isIconHide=true" value="over">
-          <mat-icon>star</mat-icon>
-        </button>
-
-        <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterMarkAsRead(); isIconHide=true" value="over">
-          <mat-icon>visibility</mat-icon>
-        </button>
-
-        <button mat-icon-button (focus)="hideCommentsList=false; isSortDesc=false" (click)="sortDesc()" value="over">
-          <mat-icon>keyboard_arrow_up</mat-icon>
-        </button>
-
-        <button mat-icon-button (focus)="hideCommentsList=false; isSortDesc=true" (click)="sort()" value="over">
-          <mat-icon>keyboard_arrow_down</mat-icon>
-        </button>
-
-        <button mat-icon-button (focus)="hideCommentsList=false; isSortByTime=false" (click)="sortTimeStamp()" value="over">
-          <mat-icon>remove</mat-icon>
-        </button>
-      </div>
-    </mat-menu>
+  <mat-icon mat-icon-button *ngIf="!searchBox.value" class="add-filterIcon" color="accent" [matMenuTriggerFor]="filterMenu">
+    filter_list
+  </mat-icon>
+
+  <mat-menu #filterMenu="matMenu" xPosition="before">
+    <div id="filterIcon">
+      <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterMarkAsCorrect(); isIconHide=true">
+        <mat-icon>check_circle</mat-icon>
+      </button>
+
+      <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterFavorite(); isIconHide=true">
+        <mat-icon>star</mat-icon>
+      </button>
+
+      <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterMarkAsRead(); isIconHide=true">
+        <mat-icon>visibility</mat-icon>
+      </button>
+
+      <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortVoteDesc()">
+        <mat-icon>keyboard_arrow_up</mat-icon>
+      </button>
+
+      <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortVote()">
+        <mat-icon>keyboard_arrow_down</mat-icon>
+      </button>
+
+      <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortTimeStamp()">
+        <mat-icon>remove</mat-icon>
+      </button>
+    </div>
+  </mat-menu>
 </div>
 
 <mat-card id="outer-card" *ngIf="hideCommentsList">
   <app-comment *ngFor="let current of filteredComments" [comment]="current"> </app-comment>
 </mat-card>
-<mat-card id="outer-card" *ngIf="!hideCommentsList">
+<mat-card *ngIf="!hideCommentsList">
   <app-comment *ngFor="let current of comments | orderBy: 'timestamp'" [comment]="current"> </app-comment>
 </mat-card>
diff --git a/src/app/components/shared/comment-list/comment-list.component.ts b/src/app/components/shared/comment-list/comment-list.component.ts
index 5b8fa0186..876bcbf2d 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -25,8 +25,6 @@ export class CommentListComponent implements OnInit {
   isIconHide: boolean;
   filteredComments: Comment[];
   userRole: UserRole;
-  isSortDesc: boolean;
-  isSortByTime: boolean;
 
   constructor(private commentService: CommentService,
     private translateService: TranslateService,
@@ -139,27 +137,22 @@ export class CommentListComponent implements OnInit {
     this.filteredComments = this.comments.filter(c => c.correct);
   }
 
-  sort(): void {
+  sortVote(): void {
     this.comments.sort((a, b) => {
-     if (a.score > b.score) {
         return a.score - b.score;
-      }
     });
   }
 
-  sortDesc(): void {
+  sortVoteDesc(): void {
     this.comments.sort((a, b) => {
-      if (a.score < b.score) {
         return b.score - a.score;
-      }
     });
   }
 
   sortTimeStamp(): void {
     this.comments.sort((a, b) => {
-      if (a.timestamp > b.timestamp) {
-        return 1;
-      }
+      const dateA = new Date(a.timestamp), dateB = new Date(b.timestamp);
+      return +dateB - +dateA;
     });
   }
 }
-- 
GitLab