Raw contact drop down should keep order in Editor.

In compact editor UI, linked raw contacts drop down should keep order.
Also, the order in the comma style list should be the same as
in the drop down.

BUG 24898494

Change-Id: Ieed213388e52c5355c5e9ad33c1ee9db40696117
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 6f5a9cb..f5ce6e3 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -30,9 +30,7 @@
 import com.android.contacts.util.UiClosables;
 
 import android.content.ContentUris;
-import android.content.ContentValues;
 import android.content.Context;
-import android.content.Intent;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.net.Uri;
@@ -74,7 +72,8 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
-import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -169,8 +168,12 @@
             // For email addresses, we don't want to truncate at end, which might cut off the domain
             // name.
             final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
-            text2.setText(accountName);
-            text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
+            if (TextUtils.isEmpty(accountName)) {
+                text2.setVisibility(View.GONE);
+            } else {
+                text2.setText(accountName);
+                text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
+            }
 
             final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
             icon.setImageDrawable(accountType.getDisplayIcon(mContext));
@@ -864,6 +867,8 @@
     private void addRawContactAccountSelector(final RawContactDeltaList rawContactDeltas) {
         mRawContactContainer.setVisibility(View.VISIBLE);
 
+        Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
+
         final String accountsSummary = getRawContactsAccountsSummary(
                 getContext(), rawContactDeltas);
         mRawContactSummary.setText(accountsSummary);
@@ -913,7 +918,7 @@
 
     private static String getRawContactsAccountsSummary(
             Context context, RawContactDeltaList rawContactDeltas) {
-        final Map<String, Integer> accountTypeNumber = new HashMap<>();
+        final LinkedHashMap<String, Integer> accountTypeNumber = new LinkedHashMap<>();
         for (RawContactDelta rawContactDelta : rawContactDeltas) {
             if (rawContactDelta.isVisible()) {
                 final AccountType accountType = rawContactDelta.getRawContactAccountType(context);
@@ -928,7 +933,7 @@
             }
         }
 
-        final Set<String> linkedAccounts = new HashSet<>();
+        final LinkedHashSet<String> linkedAccounts = new LinkedHashSet<>();
         for (String accountTypeLabel : accountTypeNumber.keySet()) {
             final String number = context.getResources().getQuantityString(
                     R.plurals.quickcontact_suggestion_account_type_number,