-2

In my Android app, I have to sort the JSON object below based on the "id" in ascending order. There will be around 2000 objects. Please let me know how to sort these JSON objects.

[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "8001.02", 
        "price_btc": "1.0", 
        "24h_volume_usd": "9892040000.0", 
        "market_cap_usd": "134838389703", 
        "available_supply": "16852650.0", 
        "total_supply": "16852650.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "-1.27", 
        "percent_change_24h": "7.28", 
        "percent_change_7d": "-20.22", 
        "last_updated": "1518071364"
    }, 
    {
        "id": "ethereum", 
        "name": "Ethereum", 
        "symbol": "ETH", 
        "rank": "2", 
        "price_usd": "803.952", 
        "price_btc": "0.100246", 
        "24h_volume_usd": "4216090000.0", 
        "market_cap_usd": "78370675159.0", 
        "available_supply": "97481784.0", 
        "total_supply": "97481784.0", 
        "max_supply": null, 
        "percent_change_1h": "-1.22", 
        "percent_change_24h": "7.16", 
        "percent_change_7d": "-29.01", 
        "last_updated": "1518071354"
    }
]
iappmaker
  • 2,945
  • 9
  • 35
  • 76

2 Answers2

1

I suggest to do it in the server side, while querying and fetching the data from database they can sort in the query itself, which will reduce the bunch of processing time(To compare 2000 or more values, the process time may increase and results in a performance issue).

If the above is not possible, then you have a solution

  1. Set each json array data in a Pojo class(Serializable class)
  2. Make a array list(Let's say it is stockList) with pojo class you have made.
  3. Let's say your pojo class name StockInfo and use the below code in your java file.

    Collections.sort(stockList, new Comparator<StockInfo>() {
        @Override
        public int compare(StockInfo s1, StockInfo s2) {
            return s1.getId().compareToIgnoreCase(s2.getId());
        }
    });
    

I hope, this solution will helps you.

Bethan
  • 971
  • 8
  • 23
0

This is sample example for list of JSON object sorting

1.the ArrayList based on the student Age property. This is how it can be done – First, implement Comparable interface and then Override the compareTo method.

public class Student implements Comparable {
    private String studentname;
    private int rollno;
    private int studentage;

    public Student(int rollno, String studentname, int studentage) {
        this.rollno = rollno;
        this.studentname = studentname;
        this.studentage = studentage;
    }
    ...
    //getter and setter methods same as the above example 
    ...
    @Override
    public int compareTo(Student comparestu) {
        int compareage=((Student)comparestu).getStudentage();
        /* For Ascending order*/
        return this.studentage-compareage;

        /* For Descending order do like this */
        //return compareage-this.studentage;
    }

    @Override
    public String toString() {
        return "[ rollno=" + rollno + ", name=" + studentname + ", age=" + studentage + "]";
    }

}

2.then Call Collections.sort on ArrayList

import java.util.*;
public class ArrayListSorting  {

    public static void main(String args[]){
       ArrayList<Student> arraylist = new ArrayList<Student>();
       arraylist.add(new Student(223, "Chaitanya", 26));
       arraylist.add(new Student(245, "Rahul", 24));
       arraylist.add(new Student(209, "Ajeet", 32));

       Collections.sort(arraylist);

       for(Student str: arraylist){
            System.out.println(str);
       }
    }
}
avinash
  • 29
  • 4