Sorting and comparting List Of Json Object

 import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Date;

import java.util.List;


import org.json.JSONException;

import org.json.JSONObject;


public class TestClass {


static DateFormat df = new SimpleDateFormat("yy-MM-dd hh:mm:ss aa");

    Date dateobj = new Date();

    

@SuppressWarnings("deprecation")

public static void main(String[] args) {

JSONObject dbObject1 = new JSONObject();

dbObject1.put("name", "abc.pdf");

dbObject1.put("UniqueId", "123-456-789-001");

dbObject1.put("createdDate", df.format(new Date(2019,8,28,10,22,11)));

dbObject1.put("modifiedDate", df.format(new Date(2020,8,28,10,22,12)));

dbObject1.put("docKey", "9876-345");

JSONObject dbObject2 = new JSONObject();

dbObject2.put("name", "abc.pdf");

dbObject2.put("UniqueId", "123-456-789-002");

dbObject2.put("createdDate", df.format(new Date(2020,8,28,10,22,13)));

dbObject2.put("modifiedDate", df.format(new Date(2020,8,28,10,22,14)));

dbObject2.put("docKey", "9876-345");

JSONObject dbObject3 = new JSONObject();

dbObject3.put("name", "abc.pdf");

dbObject3.put("UniqueId", "013-456-789-008");

dbObject3.put("createdDate", df.format(new Date(2020,8,28,10,22,11)));

dbObject3.put("modifiedDate", df.format(new Date(2020,8,28,10,22,15)));

dbObject3.put("docKey", "9876-345");

JSONObject dbObject = new JSONObject();

dbObject.put("123-456-789-001", dbObject1);

dbObject.put("123-456-789-002", dbObject2);

dbObject.put("123-456-789-002", dbObject3);

List<JSONObject> dbJsonList = new ArrayList<JSONObject>();

dbJsonList.add(dbObject1);

dbJsonList.add(dbObject2);

System.out.println("jsonList......"+dbJsonList);

System.out.println("============");

JSONObject solr1 = new JSONObject();

solr1.put("name", "abc.pdf");

solr1.put("UniqueId", "323-456-789-005");

solr1.put("createdDate", df.format(new Date(2019,8,28,10,22,11)));

solr1.put("modifiedDate", df.format(new Date(2020,8,28,10,22,13)));

solr1.put("docKey", "9876-345");

JSONObject solr2 = new JSONObject();

solr2.put("name", "abc.pdf");

solr2.put("UniqueId", "213-456-789-008");

solr2.put("createdDate", df.format(new Date(2020,8,28,10,22,11)));

solr2.put("modifiedDate", df.format(new Date(2020,8,28,10,22,11)));

solr2.put("docKey", "9876-345");

JSONObject solr3 = new JSONObject();

solr3.put("name", "abc.pdf");

solr3.put("UniqueId", "013-456-789-008");

solr3.put("createdDate", df.format(new Date(2020,8,28,10,22,11)));

solr3.put("modifiedDate", df.format(new Date(2020,8,28,10,22,10)));

solr3.put("docKey", "9876-345");

List<JSONObject> solrJsonList = new ArrayList<JSONObject>();

solrJsonList.add(solr1);

solrJsonList.add(solr2);

solrJsonList.add(solr3);

System.out.println("solrJsonList......"+solrJsonList);

System.out.println("============");

Collections.sort( solrJsonList, new Comparator<JSONObject>() {

private static final String KEY_NAME = "UniqueId";

@Override

public int compare(JSONObject o1, JSONObject o2) {

String valA = new String();

            String valB = new String();

            try {

                valA = (String) o1.get(KEY_NAME);

                valB = (String) o2.get(KEY_NAME);

            } 

            catch (JSONException e) {

                //do something

            }

            return valA.compareTo(valB);

}

});

System.out.println("solrJsonList......"+solrJsonList);

}


}


Comments

Popular posts from this blog

Install Alfresco Content Service 6.0 on ubuntu 16 using distribution zip

Lucene and fts-search

Call javascript webscript from contoller