Ville Saalo avatar Ville Saalo committed fd296ca

Sort the followers alphabetically in the RepositoryFollowersActivity.

Had to do some refactoring as the sort was case sensitive before, and while that may work with the contents of source code directories, it does not look good with usernames.

Comments (0)

Files changed (5)

src/com/saibotd/bitbeaker/Helper.java

 import android.util.Log;
 
 public class Helper {
-	public enum Sort {ASCENDING(1), DESCENDING(-1);
+	public enum Sort {ASCENDING_CASE_SENSITIVE(1, true), DESCENDING_CASE_SENSITIVE(-1, true),
+		              ASCENGING_IGNORE_CASE(1, false), DESCENDING_IGNORE_CASE(1, false);
 		private int factor;
-		Sort(int f) {
-			factor = f;
+		private boolean caseSensitive;
+		Sort(int f, boolean caseSensitive) {
+			this.factor = f;
+			this.caseSensitive = caseSensitive;
 		}
 		public int getFactor() {
 			return factor;
 		}
+		public boolean isCaseSensitive() {
+			return caseSensitive;
+		}
 	};
 	
 	private static Context context;
 	 * @return A JSONArray with its contents sorted by the given parameters
 	 */
 	public static JSONArray getSortedJsonObjectArray(JSONArray array, String key, Sort direction) {
-		final String[] params = {key};
-		final Sort[] dirs = {direction};
-		return getSortedJsonObjectArray(array, params, dirs);
+		return getSortedJsonObjectArray(array, new SortOrder(key, direction));
 	}
 	
 	/**
 	 * Sorts a JSONArray that consists of JSONObjects. Multiple sort keys can be defined.
 	 * @param array A JSONArray that contains only JSONObjects
-	 * @param keys The keys to the values by which to sort
-	 * @param directions The directions on how to sort by each of those aforementioned 
-	 *                   values: ascending or descending. If this array is shorter than
-	 *                   the array of keys, then the default sort order for the remaining
-	 *                   keys is ascending.
+	 * @param sortOrders The keys and directions on how to sort the array. 
 	 * @return A JSONArray with its contents sorted by the given parameters
 	 */
-	public static JSONArray getSortedJsonObjectArray(JSONArray array, String[] keys, Sort[] directions) {
+	public static JSONArray getSortedJsonObjectArray(JSONArray array, SortOrder... sortOrders) {
 		try {
 			List<JSONObject> list = getJsonArrayAsList(array);
 			
-			Collections.sort(list, new JSONObjectComparator(keys, directions));
+			Collections.sort(list, new JSONObjectComparator(sortOrders));
 			
 			return getListAsJsonArray(list);
 		} catch (JSONException e) {
 	 * {@link java.util.Collections#sort(List, Comparator)} method. 
 	 */
 	private static class JSONObjectComparator implements Comparator<JSONObject> {
-		private final String[] parameters;
-		private final Sort[] directions;
+		private final SortOrder[] sortOrders;
 
-		public JSONObjectComparator(String[] params, Sort[] dirs) {
-			parameters = params;
-			directions = dirs;
+		public JSONObjectComparator(SortOrder... orders) {
+			sortOrders = orders;
 		}
 
 		@Override
 		public int compare(JSONObject a, JSONObject b) {
 			try {
 				int result = 0;
-				for (int i = 0; i < parameters.length; i++) {
-					Sort dir = i < directions.length ? directions[i] : Sort.ASCENDING;
-					result = a.getString(parameters[i]).compareTo(b.getString(parameters[i])) * dir.getFactor();
+				for (int i = 0; i < sortOrders.length; i++) {
+					final SortOrder order = sortOrders[i];
+					final String value1 = a.getString(order.key);
+					final String value2 = b.getString(order.key);
+					final int factor = order.direction.getFactor();
+					if (order.direction.isCaseSensitive()) {
+						result = value1.compareTo(value2) * factor;
+					} else {
+						result = value1.compareToIgnoreCase(value2) * factor;
+					}
 					if (result != 0) {
 						break;
 					}

src/com/saibotd/bitbeaker/IssuesActivity.java

 			else{
 				final String SORT_ISSUES_BY = "utc_last_updated";
 				listView.setAdapter(new IssuesAdapter(this, 
-						Helper.getSortedJsonObjectArray(json.getJSONArray("issues"), SORT_ISSUES_BY, Sort.DESCENDING)));
+						Helper.getSortedJsonObjectArray(json.getJSONArray("issues"), SORT_ISSUES_BY, Sort.DESCENDING_CASE_SENSITIVE)));
 				listView.setVisibility(ListView.VISIBLE);
 			}
 		} catch (JSONException e) {

src/com/saibotd/bitbeaker/RepositoryFollowersActivity.java

 import android.widget.TextView;
 
 import com.actionbarsherlock.app.ActionBar;
+import com.saibotd.bitbeaker.Helper.Sort;
 
 public class RepositoryFollowersActivity extends MyActivity {
 
 		ListView listView = (ListView) findViewById(R.id.repositories_list);
 		try {
 			JSONObject jsonObject = new JSONObject(result);
-			followers = jsonObject.getJSONArray("followers");
+			followers = Helper.getSortedJsonObjectArray(jsonObject.getJSONArray("followers"), "username", Sort.ASCENGING_IGNORE_CASE);
 		} catch (JSONException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();

src/com/saibotd/bitbeaker/SearchableActivity.java

 		try {
 			lastResult = new JSONObject(result).getJSONArray("repositories");
 			if(lastResult.length() == 0) makeToast(R.string.no_repositories_found);
-			final String[] sortKeys = {"is_fork", "utc_last_updated"};
-			final Sort[] sortDirs = {Sort.ASCENDING, Sort.DESCENDING};
-			listView.setAdapter(new SearchableRepositoriesAdapter(this, Helper.getSortedJsonObjectArray(lastResult, sortKeys, sortDirs)));
+			listView.setAdapter(new SearchableRepositoriesAdapter(this, Helper.getSortedJsonObjectArray(lastResult, 
+																	new SortOrder("is_fork", Sort.ASCENDING_CASE_SENSITIVE),
+																	new SortOrder("utc_last_updated", Sort.DESCENDING_CASE_SENSITIVE))));
 		} catch (JSONException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();

src/com/saibotd/bitbeaker/tests/HelperTest.java

 
 import com.saibotd.bitbeaker.Helper;
 import com.saibotd.bitbeaker.Helper.Sort;
+import com.saibotd.bitbeaker.SortOrder;
 
 public class HelperTest extends TestCase {
 	public void test_isEmpty_with_empty_results() {
 	}
 	
 	public void test_getSortedJsonObjectArray_with_ascending_date() throws Exception {
-		JSONArray result = Helper.getSortedJsonObjectArray(getTestArray(), "date", Sort.ASCENDING);
+		JSONArray result = Helper.getSortedJsonObjectArray(getTestArray(), "date", Sort.ASCENDING_CASE_SENSITIVE);
 		int i = 0;
 		assertEquals("1917-12-06", result.getJSONObject(i).get("date"));
 		assertEquals("Finland becomes independent", result.getJSONObject(i++).get("event"));
 	}
 	
 	public void test_getSortedJsonObjectArray_with_descending_text() throws Exception {
-		JSONArray result = Helper.getSortedJsonObjectArray(getTestArray(), "event", Sort.DESCENDING);
+		JSONArray result = Helper.getSortedJsonObjectArray(getTestArray(), "event", Sort.DESCENDING_CASE_SENSITIVE);
 		int i = 0;
 		assertEquals("1991-12-08", result.getJSONObject(i).get("date"));
 		assertEquals("USSR dissolves", result.getJSONObject(i++).get("event"));
 	}
 	
 	public void test_getSortedJsonObjectArray_with_two_sort_keys_and_directions() throws Exception {
-		final String[] keys = {"num", "txt"};
-		final Sort[] directions = {Sort.DESCENDING, Sort.ASCENDING};
-		JSONArray result = Helper.getSortedJsonObjectArray(getAnotherTestArray(), keys, directions);
+		JSONArray result = Helper.getSortedJsonObjectArray(getAnotherTestArray(),
+															new SortOrder("num", Sort.DESCENDING_CASE_SENSITIVE),
+															new SortOrder("txt", Sort.ASCENDING_CASE_SENSITIVE));
 		
 		int i = 0;
 		assertEquals("300", result.getJSONObject(i).get("num"));
 	}
 	
 	public void test_getSortedJsonObjectArray_with_two_sort_keys_and_directions_inverted() throws Exception {
-		final String[] keys = {"num", "txt"};
-		final Sort[] directions = {Sort.ASCENDING, Sort.DESCENDING};
-		JSONArray result = Helper.getSortedJsonObjectArray(getAnotherTestArray(), keys, directions);
+		JSONArray result = Helper.getSortedJsonObjectArray(getAnotherTestArray(), 
+															new SortOrder("num", Sort.ASCENDING_CASE_SENSITIVE),
+															new SortOrder("txt", Sort.DESCENDING_CASE_SENSITIVE));
 		
 		int i = 0;
 		assertEquals("100", result.getJSONObject(i).get("num"));
 	}
 	
 	public void test_getSortedJsonObjectArray_with_two_sort_keys_but_just_one_direction() throws Exception {
-		final String[] keys = {"txt", "num"};
-		final Sort[] directions = {Sort.ASCENDING};
-		JSONArray result = Helper.getSortedJsonObjectArray(getAnotherTestArray(), keys, directions);
+		JSONArray result = Helper.getSortedJsonObjectArray(getAnotherTestArray(), 
+															new SortOrder("txt", Sort.ASCENDING_CASE_SENSITIVE),
+															new SortOrder("num"));
 		
 		int i = 0;
 		assertEquals("A", result.getJSONObject(i).get("txt"));
 		assertEquals("100", result.getJSONObject(i++).get("num"));
 	}
 	
+	public void test_getSortedJsonObjectArray_with_case_insensitive_sort() throws Exception {
+		JSONArray array = new JSONArray();
+		JSONObject o1 = new JSONObject();
+		JSONObject o2 = new JSONObject();
+		JSONObject o3 = new JSONObject();
+		JSONObject o4 = new JSONObject();
+		JSONObject o5 = new JSONObject();
+		o1.put("text", "Alfa");
+		o2.put("text", "bravo");
+		o3.put("text", "CHARLIE");
+		o4.put("text", "delta");
+		o5.put("text", "EchO");
+		array.put(o1);
+		array.put(o3);
+		array.put(o4);
+		array.put(o2);
+		array.put(o5);
+
+		JSONArray result = Helper.getSortedJsonObjectArray(array, "text", Sort.ASCENGING_IGNORE_CASE);
+		int i = 0;
+		assertEquals("Alfa", result.getJSONObject(i++).getString("text"));
+		assertEquals("bravo", result.getJSONObject(i++).getString("text"));
+		assertEquals("CHARLIE", result.getJSONObject(i++).getString("text"));
+		assertEquals("delta", result.getJSONObject(i++).getString("text"));
+		assertEquals("EchO", result.getJSONObject(i++).getString("text"));
+	}
+	
 	private JSONArray getTestArray() throws Exception {
 		JSONArray array = new JSONArray();
 		JSONObject o1 = new JSONObject();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.