Bug 804650 - [contacts] API sorting problems. r=shu a=blocking-basecamp
authorGregor Wagner <anygregor@gmail.com>
Tue, 30 Oct 2012 12:04:58 -0700
changeset 116491 0859decaeafcbdcfc168780acf7069d9102e9ce1
parent 116490 18d116f5ceda972f0a5345069811dd9099a71881
child 116492 862bc295a7bb55043c468809b8f3bdbc6cb3a34d
push id1708
push userakeybl@mozilla.com
push dateMon, 19 Nov 2012 21:10:21 +0000
treeherdermozilla-beta@27b14fe50103 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshu, blocking-basecamp
bugs804650
milestone18.0a2
Bug 804650 - [contacts] API sorting problems. r=shu a=blocking-basecamp
dom/contacts/fallback/ContactService.jsm
--- a/dom/contacts/fallback/ContactService.jsm
+++ b/dom/contacts/fallback/ContactService.jsm
@@ -74,48 +74,44 @@ let DOMContactManager = {
 
     /*
      * Sorting the contacts by sortBy field. sortBy can either be familyName or givenName.
      * If 2 entries have the same sortyBy field or no sortBy field is present, we continue 
      * sorting with the other sortyBy field.
      */
     function sortfunction(a, b){
       let x, y;
-      let sortByNameSet = true;
       let result = 0;
       let findOptions = msg.options.findOptions;
-      let sortBy = findOptions.sortBy;
       let sortOrder = findOptions.sortOrder;
-      
-      if (!a.properties[sortBy] || !(x = a.properties[sortBy][0].toLowerCase())) {
-        sortByNameSet = false;
-      }
+      let sortBy = findOptions.sortBy === "familyName" ? [ "familyName", "givenName" ] : [ "givenName" , "familyName" ];
+      let xIndex = 0;
+      let yIndex = 0;
 
-      if (!b.properties[sortBy] || !(y = b.properties[sortBy][0].toLowerCase())) {
-        if (sortByNameSet) {
+      do {
+        while (xIndex < sortBy.length && !x) {
+          x = a.properties[sortBy[xIndex]] ? a.properties[sortBy[xIndex]][0].toLowerCase() : null;
+          xIndex++;
+        }
+        if (!x) {
           return sortOrder == 'ascending' ? 1 : -1;
         }
-      }
-
-      if (sortByNameSet) {
-        result = x.localeCompare(y);
-      }
-
-      if (result == 0) {
-        // If 2 entries have the same sortBy (familyName or givenName) field,
-        // we have to continue sorting.
-        let otherSortBy = sortBy == "familyName" ? "givenName" : "familyName";
-        if (!a.properties[otherSortBy] || !(x = a.properties[otherSortBy][0].toLowerCase())) {
+        while (yIndex < sortBy.length && !y) {
+          y = b.properties[sortBy[yIndex]] ? b.properties[sortBy[yIndex]][0].toLowerCase() : null;
+          yIndex++;
+        }
+        if (!y) {
           return sortOrder == 'ascending' ? 1 : -1;
         }
-        if (!b.properties[otherSortBy] || !(y = b.properties[otherSortBy][0].toLowerCase())) {
-          return sortOrder == 'ascending' ? 1 : -1;
-        }
+
         result = x.localeCompare(y);
-      }
+        x = null;
+        y = null;
+      } while (result === 0);
+
       return sortOrder == 'ascending' ? result : -result;
     }
 
     switch (aMessage.name) {
       case "Contacts:Find":
         let result = new Array();
         this._db.find(
           function(contacts) {