Note that there are some explanatory texts on larger screens.

plurals
  1. POSort my ojects which are parsed from json data
    text
    copied!<p>My Android client get server JSON response as follows:</p> <pre><code>{"students":[{"id":1,"name":"John","age":12}, {"id":2,"name":"Thmas","age":13} {"id":3,"name":"Merit","age":10} ...]} </code></pre> <p>My Android client code parses the JSON response to Java object by using <a href="http://code.google.com/p/google-gson/">gson</a>.</p> <p>My corresponding Java classes:</p> <pre><code>public class StudentList{ private List&lt;Student&gt; students; public List&lt;Student&gt; getStudents(){ return students; } } public class Student{ private long id; private String name; private int age; public long getId() { return id; } public String getName(){ return name; } public int getAge(){ return age; } } </code></pre> <p>Everything works fine for me at this point, I can successfully parse JSON data to my Java objects, like following:</p> <pre><code>//'jsonData' is the server responsed json data StudentList students = gson.fromJson(jsonData, StudentList.class) </code></pre> <p>Then, I would like to modify a bit to get the <code>students</code> (from <code>json</code> data) in an <strong>alphabetic order, sorted by</strong> student's <code>name</code>. I tried the following way: (I changed the <code>Student</code> class to implement the <code>Comparable&lt;&gt;</code> interface):</p> <pre><code>public class Student implements Comparable&lt;Student&gt;{ private long id; private String name; private int age; public long getId() { return id; } public String getName(){ return name; } public int getAge(){ return age; } // Override compareTo(), sort by 'name' @Override public int compareTo(Student obj) { return this.getName().compareToIgnoreCase(obj.Name()); } } </code></pre> <p>With above modified <code>Student</code> class, I tried to parse the json data again :</p> <pre><code>//'jsonData' is the server responsed json data StudentList students = gson.fromJson(jsonData, StudentList.class) </code></pre> <p>But the resulted <code>students</code> are still the same as before the sorting. My sorting solution does not work at all. <strong>Why?? Where am I wrong??</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload