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

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") : ""; = getAuthor(jsonObject); = jsonObject.has("first_publish_year") ? jsonObject.getString("first_publish_year") : "";
} catch (JSONException e) {
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, specifically in this:

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(;
viewHolder.tvTitle = (TextView) convertView.findViewById(;
viewHolder.tvAuthor = (TextView) convertView.findViewById(;
viewHolder.tvDate = (TextView) convertView.findViewById(;
} else {
viewHolder = (ViewHolder) convertView.getTag();
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.