Skip to content

Commit

Permalink
Sort contacts that start with a number at the end.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jul 19, 2021
1 parent 167a691 commit bfdebbf
Showing 1 changed file with 12 additions and 2 deletions.
Expand Up @@ -2378,7 +2378,7 @@ public void updateSystemContactColors() {
.excludeId(includeSelf ? null : Recipient.self().getId())
.build();

String orderBy = SORT_NAME + ", " + SYSTEM_JOINED_NAME + ", " + SEARCH_PROFILE_NAME + ", " + USERNAME + ", " + PHONE;
String orderBy = orderByPreferringAlphaOverNumeric(SORT_NAME) + ", " + PHONE;

return databaseHelper.getReadableDatabase().query(TABLE_NAME, SEARCH_PROJECTION, searchSelection.where, searchSelection.args, null, null, orderBy);
}
Expand All @@ -2395,7 +2395,7 @@ public void updateSystemContactColors() {

String selection = searchSelection.getWhere();
String[] args = searchSelection.getArgs();
String orderBy = SORT_NAME + ", " + SYSTEM_JOINED_NAME + ", " + SEARCH_PROFILE_NAME + ", " + PHONE;
String orderBy = orderByPreferringAlphaOverNumeric(SORT_NAME) + ", " + PHONE;

return databaseHelper.getReadableDatabase().query(TABLE_NAME, SEARCH_PROJECTION, selection, args, null, null, orderBy);
}
Expand Down Expand Up @@ -3091,6 +3091,16 @@ private void clearSystemDataForPendingInfo() {
return "NULLIF(" + column + ", '')";
}

/**
* By default, SQLite will prefer numbers over letters when sorting. e.g. (b, a, 1) is sorted as (1, a, b).
* This order by will using a GLOB pattern to instead sort it as (a, b, 1).
*
* @param column The name of the column to sort by
*/
private static @NonNull String orderByPreferringAlphaOverNumeric(@NonNull String column) {
return "CASE WHEN " + column + " GLOB '[0-9]*' THEN 1 ELSE 0 END, " + column;
}

private static @NonNull String removeWhitespace(@NonNull String column) {
return "REPLACE(" + column + ", ' ', '')";
}
Expand Down

0 comments on commit bfdebbf

Please sign in to comment.