Bug 1639750 - Fix sorting of address book contact list (attempt 2). r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 29 Jun 2020 13:02:46 +0300
changeset 38877 2e8aebfa1995801e26ace53d47b6751e20ce969e
parent 38876 ab5e24a58ebb96dfdcb6d00ea84e42ebb560f6e7
child 38878 376d4a7f56f68beed993e202b2cec4a0e9a7857c
push id2646
push userclokep@gmail.com
push dateMon, 29 Jun 2020 21:33:35 +0000
treeherdercomm-beta@b1e911288dbd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs1639750
Bug 1639750 - Fix sorting of address book contact list (attempt 2). r=mkmelin
mail/components/addrbook/test/browser/browser_contact_tree.js
mailnews/addrbook/content/abView.js
--- a/mail/components/addrbook/test/browser/browser_contact_tree.js
+++ b/mail/components/addrbook/test/browser/browser_contact_tree.js
@@ -274,17 +274,19 @@ add_task(async () => {
       ""
     );
   }
 
   let bookA = createAddressBook("book A");
   openDirectory(bookA);
   checkRows();
   let contactA2 = bookA.addCard(createContact("contact", "A2"));
+  checkRows(contactA2);
   let contactA1 = bookA.addCard(createContact("contact", "A1")); // Add first.
+  checkRows(contactA1, contactA2);
   let contactA5 = bookA.addCard(createContact("contact", "A5")); // Add last.
   checkRows(contactA1, contactA2, contactA5);
   let contactA3 = bookA.addCard(createContact("contact", "A3")); // Add in the middle.
   checkRows(contactA1, contactA2, contactA3, contactA5);
 
   // Flip sort direction.
   EventUtils.synthesizeMouseAtCenter(
     abContactTree.columns.GeneratedName.element,
--- a/mailnews/addrbook/content/abView.js
+++ b/mailnews/addrbook/content/abView.js
@@ -166,37 +166,31 @@ ABView.prototype = {
         if (!this.directory) {
           break;
         }
       // Falls through.
       case "addrbook-contact-created":
         let viewCard = new abViewCard(subject);
         let sortText = viewCard.getText(this.sortColumn);
         let added = false;
-        for (let i = this._rowMap.length - 1; !added && i >= 0; i--) {
+        for (let i = 0; !added && i < this._rowMap.length; i++) {
+          let comparison = this.collator.compare(
+            sortText,
+            this._rowMap[i].getText(this.sortColumn)
+          );
           if (
-            this.collator.compare(
-              sortText,
-              this._rowMap[i].getText(this.sortColumn)
-            ) < 0
+            (comparison < 0 && this.sortDirection == "ascending") ||
+            (comparison >= 0 && this.sortDirection == "descending")
           ) {
-            this._rowMap.splice(
-              this.sortDirection == "ascending" ? i : i + 1,
-              0,
-              viewCard
-            );
+            this._rowMap.splice(i, 0, viewCard);
             added = true;
           }
         }
         if (!added) {
-          if (this.sortDirection == "ascending") {
-            this._rowMap.push(viewCard);
-          } else {
-            this._rowMap.unshift(viewCard);
-          }
+          this._rowMap.push(viewCard);
         }
         if (this.listener) {
           this.listener.onCountChanged(this.rowCount);
         }
         break;
 
       case "addrbook-list-updated":
         if (!this.directory) {