Merge "Re-use sortComments from gr-comment-api in diff-host"
diff --git a/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts b/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
index b49f522..ba37387 100644
--- a/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
+++ b/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.ts
@@ -45,6 +45,7 @@
 export interface HumanCommentInfoWithPath extends CommentInfo {
   path: string;
   __draft?: boolean;
+  __date?: Date;
 }
 
 export interface RobotCommentInfoWithPath extends RobotCommentInfo {
@@ -80,6 +81,27 @@
   return !!(x as PatchSetFile).path;
 }
 
+export function sortComments<
+  T extends CommentInfoWithPath | CommentInfoWithTwoPaths
+>(comments: T[]): T[] {
+  return comments.slice(0).sort((c1, c2) => {
+    const d1 = !!(c1 as HumanCommentInfoWithPath).__draft;
+    const d2 = !!(c2 as HumanCommentInfoWithPath).__draft;
+    if (d1 !== d2) return d1 ? 1 : -1;
+    const date1 =
+      (c1.updated && parseDate(c1.updated)) ||
+      (c1 as HumanCommentInfoWithPath).__date;
+    const date2 =
+      (c2.updated && parseDate(c2.updated)) ||
+      (c2 as HumanCommentInfoWithPath).__date;
+    const dateDiff = date1.valueOf() - date2.valueOf();
+    if (dateDiff) {
+      return dateDiff;
+    }
+    return c1.id < c2.id ? -1 : c1.id > c2.id ? 1 : 0;
+  });
+}
+
 export interface CommentThread {
   comments: CommentInfoWithTwoPaths[];
   patchNum?: PatchSetNum;
@@ -519,7 +541,7 @@
     // However, this doesn't affect the final result of computeUnresolvedNum
     // This should be fixed by removing CommentInfoWithTwoPaths later
     const threads = this.getCommentThreads(
-      this._sortComments(comments) as CommentInfoWithTwoPaths[]
+      sortComments(comments) as CommentInfoWithTwoPaths[]
     );
 
     const unresolvedThreads = threads.filter(
@@ -533,26 +555,10 @@
 
   getAllThreadsForChange() {
     const comments = this._commentObjToArrayWithFile(this.getAllComments(true));
-    const sortedComments = this._sortComments(comments);
+    const sortedComments = sortComments(comments);
     return this.getCommentThreads(sortedComments);
   }
 
-  _sortComments<T extends CommentInfoWithPath | CommentInfoWithTwoPaths>(
-    comments: T[]
-  ): T[] {
-    return comments.slice(0).sort((c1, c2) => {
-      const d1 = !!(c1 as HumanCommentInfoWithPath).__draft;
-      const d2 = !!(c2 as HumanCommentInfoWithPath).__draft;
-      if (d1 !== d2) return d1 ? 1 : -1;
-      const dateDiff =
-        parseDate(c1.updated).valueOf() - parseDate(c2.updated).valueOf();
-      if (dateDiff) {
-        return dateDiff;
-      }
-      return c1.id < c2.id ? -1 : c1.id > c2.id ? 1 : 0;
-    });
-  }
-
   /**
    * Computes all of the comments in thread format.
    *
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
index 7693d56..02d8cfa 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
@@ -24,11 +24,11 @@
 import {PolymerElement} from '@polymer/polymer/polymer-element.js';
 import {htmlTemplate} from './gr-diff-host_html.js';
 import {GrDiffBuilder} from '../gr-diff-builder/gr-diff-builder.js';
-import {parseDate} from '../../../utils/date-util.js';
 import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
 import {DiffSide, rangesEqual} from '../gr-diff/gr-diff-utils.js';
 import {appContext} from '../../../services/app-context.js';
 import {getParentIndex, isMergeParent} from '../../../utils/patch-set-util.js';
+import {sortComments} from '../gr-comment-api/gr-comment-api.js';
 
 const MSG_EMPTY_BLAME = 'No blame information for this diff.';
 
@@ -692,20 +692,12 @@
     }
   }
 
-  _sortComments(comments) {
-    return comments.slice(0).sort((a, b) => {
-      if (b.__draft && !a.__draft ) { return -1; }
-      if (a.__draft && !b.__draft ) { return 1; }
-      return parseDate(a.updated) - parseDate(b.updated);
-    });
-  }
-
   /**
    * @param {!Array<!Object>} comments
    * @return {!Array<!Object>} Threads for the given comments.
    */
   _createThreads(comments) {
-    const sortedComments = this._sortComments(comments);
+    const sortedComments = sortComments(comments);
     const threads = [];
     for (const comment of sortedComments) {
       // If the comment is in reply to another comment, find that comment's
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.js b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.js
index cd43fdb..11539b1 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.js
@@ -21,6 +21,7 @@
 import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
 import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
 import {DiffSide} from '../gr-diff/gr-diff-utils.js';
+import {sortComments} from '../gr-comment-api/gr-comment-api.js';
 
 const basicFixture = fixtureFromElement('gr-diff-host');
 
@@ -1131,7 +1132,7 @@
         in_reply_to: 'sallys_confession',
       },
     ];
-    const sortedComments = element._sortComments(comments);
+    const sortedComments = sortComments(comments);
     assert.equal(sortedComments[0], comments[1]);
     assert.equal(sortedComments[1], comments[2]);
     assert.equal(sortedComments[2], comments[0]);
diff --git a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.js b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.js
index 5de7a66..661da6f 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.js
+++ b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.js
@@ -24,11 +24,11 @@
 import {PolymerElement} from '@polymer/polymer/polymer-element.js';
 import {htmlTemplate} from './gr-comment-thread_html.js';
 import {KeyboardShortcutMixin} from '../../../mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin.js';
-import {parseDate} from '../../../utils/date-util.js';
 import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
 import {appContext} from '../../../services/app-context.js';
 import {SpecialFilePath} from '../../../constants/constants.js';
 import {computeDisplayPath} from '../../../utils/path-list-util.js';
+import {sortComments} from '../../diff/gr-comment-api/gr-comment-api.js';
 
 const UNRESOLVED_EXPAND_COUNT = 5;
 const NEWLINE_PATTERN = /\n/g;
@@ -273,7 +273,7 @@
   }
 
   _commentsChanged() {
-    this._orderedComments = this._sortedComments(this.comments);
+    this._orderedComments = sortComments(this.comments);
     this.updateThreadProperties();
   }
 
@@ -342,22 +342,6 @@
     }
   }
 
-  _sortedComments(comments) {
-    return comments.slice().sort((c1, c2) => {
-      const c1Date = c1.__date || parseDate(c1.updated);
-      const c2Date = c2.__date || parseDate(c2.updated);
-      const dateCompare = c1Date - c2Date;
-      // Ensure drafts are at the end. There should only be one but in edge
-      // cases could be more. In the unlikely event two drafts are being
-      // compared, use the typical date compare.
-      if (c2.__draft && !c1.__draft ) { return -1; }
-      if (c1.__draft && !c2.__draft ) { return 1; }
-      if (dateCompare === 0 && (!c1.id || !c1.id.localeCompare)) { return 0; }
-      // If same date, fall back to sorting by id.
-      return dateCompare ? dateCompare : c1.id.localeCompare(c2.id);
-    });
-  }
-
   _createReplyComment(content, opt_isEditing,
       opt_unresolved) {
     this.reporting.recordDraftInteraction();
diff --git a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread_test.js b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread_test.js
index f451527..7c176ac 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread_test.js
+++ b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread_test.js
@@ -19,6 +19,7 @@
 import './gr-comment-thread.js';
 import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
 import {SpecialFilePath} from '../../../constants/constants.js';
+import {sortComments} from '../../diff/gr-comment-api/gr-comment-api.js';
 
 const basicFixture = fixtureFromElement('gr-comment-thread');
 
@@ -69,7 +70,7 @@
           updated: '2015-12-24 15:00:20.396000000',
         },
       ];
-      const results = element._sortedComments(comments);
+      const results = sortComments(comments);
       assert.deepEqual(results, [
         {
           id: 'sally_to_dr_finklestein',