I have some JSON data which I am pulling down from a server. One of the fields in this data is a distance value. I need to have the data sorted by distance from lowest to highest in the ListView. I am not sure how to go about it?
3 Answers
I don't know exactly what you want to do but there are few steps that are needed after you have the json response.
- Make json data to list of objects
- Sort the list
- Set listview's adapter with sorted list
For future questions please separate your question to different smaller but concretized questions.
1. Make json data to list of objects
Object data model
class YourDataModel {
int distance;
.....
public int getDistance(){
return this.distance;
}
public void setDistance(int distance){
this.distance = distance;
}
}
Then let's say getJsonResponse() returns your json with all the data for the list. And let's make that json list to List :D
String json = getJsonResponse(); // Change that line with your code
Gson gson = new Gson();
Type listType = new TypeToken<List<YourDataModel>>(){}.getType();
ArrayList<YourDataModel> data = (ArrayList<YourDataModel>) gson.fromJson(jsonOutput, listType);
2. Sort the list
Now let's do some magic :D data is my array with the items inside.
Collections.sort(data, new Comparator<YourDataModel>() {
@Override
public int compare(YourDataModel data1, YourDataModel data2) {
if( data1.getDistance() < data2.getDistance() )
return 1;
else
return 0;
}
});
3. Set list adapter with the sorted list to the listview
I don't know what to describe here :D
ListView lvData = (ListView) findViewById(R.id.listview1);
MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.listview_item_row, data);
lvData.setAdapter(adapter);
I don't know if that's what you want but this is how it need to be done. If you want to sort the data directly from the listview read this: Sort listview with array adapter

- 1
- 1

- 1,020
- 10
- 21
Use this and you are done:
private void sortAscending() {
Collections.sort(productList, new AscendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "sortAscending" + productList);
/*for (ProductModel s : productList){
Log.d("My array list content: ", s.getPrice()+"::"+s.getName());
}*/
}
private void sortDescending() {
Collections.sort(productList, new DescendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "productList" + productList);
for (ProductModel s : productList) {
Log.d("My array list content: ", s.getPrice() + "::" + s.getName());
}
}
public class AscendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM1 > pM2){
return 1;
}else if((pM1 < pM2)){
return -1;
}else{
return 0;
}
}
}
public class DescendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
//Log.d("hightolow","productList"+p2.getPrice().compareTo(p1.getPrice()));
// Log.d("hightolow","productList:"+p1.getPrice());
// Log.d("hightolow","productList::"+p2.getPrice());
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM2 > pM1){
return 1;
}else if((pM2 < pM1)){
return -1;
}else{
return 0;
}
}
}

- 55,572
- 24
- 139
- 212

- 849
- 10
- 8
public void sortByLocation() {
Collections.sort(list, new Comparator<PendingJobInfo>() {
@Override
public int compare(PendingJobInfo lhs, PendingJobInfo rhs) {
// TODO Auto-generated method stub
Double f1 = lhs.getDistance();
Double f2 = rhs.getDistance();
return f1.compareTo(f2);
}
});
}

- 1