Android/Java BookSearch - How do I get JSON dates to show in ascending order?

I’m currently working on an Android project using Android Studio. It uses the Open Library API and uses CodePath’s BookSearch exercise as a template.

Currently, I’m using the Open Library’s Author Search API and I’m trying to make it so that, when you search an author’s name, it gives you their books in order of release.

Here’s the GitHub repository, and the specific file I’m trying to edit is Book.java.

Now, as it is, I’m using the API’s first_publish_year to get the year to display.

"first_publish_year": 1937,

To do so, I’ve made it a private string variable:

private String date;

And in the public static class fromJson, within the try block, I use this:

public static Book fromJson(JSONObject jsonObject) {
Book book = new Book();
try {
if (jsonObject.has("cover_edition_key")) {
book.openLibraryId = jsonObject.getString("cover_edition_key");
} else if (jsonObject.has("edition_key")) {
final JSONArray ids = jsonObject.getJSONArray("edition_key");
book.openLibraryId = ids.getString(0);
}
book.title = jsonObject.has("title_suggest") ? jsonObject.getString("title_suggest") : "";
book.author = getAuthor(jsonObject);
book.date = jsonObject.has("first_publish_year") ? jsonObject.getString("first_publish_year") : "";
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return book;
}

However, I realize that is not the best way to do it and I am trying to make a private static getData to do what I need it to do.

Now, I realize that first_publish_year is actually an integer. However, whenever I change “private String date” to “private int date” (and the subseqent necessary changes for getDate and in the fromJson class), it causes an error or app crash in BookAdapter.java, specifically in this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Book book = getItem(position);
ViewHolder viewHolder;
if(convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_book, parent, false);
viewHolder.ivCover = (ImageView) convertView.findViewById(R.id.ivBookCover);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvAuthor = (TextView) convertView.findViewById(R.id.tvAuthor);
viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvTitle.setText(book.getTitle());
viewHolder.tvAuthor.setText(book.getAuthor());
viewHolder.tvDate.setText(book.getDate());
Picasso.with(getContext()).load(Uri.parse(book.getCoverUrl())).error(R.drawable.ic_nocover).into(viewHolder.ivCover);
return convertView;
}

In any case, what I’m ultimately trying to accomplish is to manipulate the JSON data via first_publish_year so that the results show the books in ascending order of release (ex. 1997 to 2007 and so forth).

Now, I have searched for and tried answers I’ve found on Stackoverflow on how to do this, specifically the answer here. However, when I change date to a private JSONObject or JSONArray (and do the necessary adjustments where needed), it causes an error in “public static ArrayList<Book> fromJson”, telling me that JSONObject cannot be applied to JSONArray or something like that.

So, to put it short, I’m stuck at the moment. I’m pretty positive that something like this can be done; I just don’t know if I’m going about it wrong or trying to edit the wrong file… or maybe the API just doesn’t allow for something like this.

Any help would be appreciated.