From 9ae8303b1831c03f1d45e6f2bab91162e8e92c04 Mon Sep 17 00:00:00 2001 From: jendib Date: Thu, 15 Jan 2015 01:57:29 +0100 Subject: [PATCH] Android: sort tags by count desc --- .../docs/adapter/FilePagerAdapter.java | 6 ++++ .../sismics/docs/adapter/TagListAdapter.java | 28 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/FilePagerAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/FilePagerAdapter.java index bff09062..e952ee35 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/FilePagerAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/FilePagerAdapter.java @@ -45,6 +45,12 @@ public class FilePagerAdapter extends PagerAdapter { */ private String authToken; + /** + * File pager adapter. + * + * @param context Context + * @param filesArray Files + */ public FilePagerAdapter(Context context, JSONArray filesArray) { this.files = new ArrayList<>(); for (int i = 0; i < filesArray.length(); i++) { diff --git a/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java b/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java index 4924234e..0c124735 100644 --- a/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java +++ b/docs-android/app/src/main/java/com/sismics/docs/adapter/TagListAdapter.java @@ -16,6 +16,11 @@ import com.sismics.docs.R; import org.json.JSONArray; import org.json.JSONObject; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + /** * Tag list adapter. * @@ -25,25 +30,36 @@ public class TagListAdapter extends BaseAdapter { /** * Tags. */ - private JSONArray tags; + private List tags; /** * Tag list adapter. * - * @param tags Tags + * @param tagsArray Tags */ - public TagListAdapter(JSONArray tags) { - this.tags = tags; + public TagListAdapter(JSONArray tagsArray) { + this.tags = new ArrayList<>(); + for (int i = 0; i < tagsArray.length(); i++) { + tags.add(tagsArray.optJSONObject(i)); + } + + // Sort tags by count desc + Collections.sort(tags, new Comparator() { + @Override + public int compare(JSONObject lhs, JSONObject rhs) { + return lhs.optInt("count") < rhs.optInt("count") ? 1 : -1; + } + }); } @Override public int getCount() { - return tags.length(); + return tags.size(); } @Override public JSONObject getItem(int position) { - return tags.optJSONObject(position); + return tags.get(position); } @Override