Sort a field of an object in Jsonarray in Java

Source: Internet
Author: User

Demand:

Call a third-party interface today, and then return a josn array that needs to be sorted from the big to the small according to the "Report date" Report date inside, the newest row is in front.

This other person's interface has been developed and cannot be changed, so only we can sort it ourselves.

Idea one (): Convert the JOSN array to the list collection, then get to each object inside, get "Report date", then sort, and sort the corresponding objects.

Idea two:

Implement the Java Collections.sort method, customize the comparator method, and then sort.

This is a high efficiency.

Here is the test and implementation code.

Import java.util.arraylist;import java.util.collections;import java.util.comparator;import  java.util.list;import com.alibaba.fastjson.json;import com.alibaba.fastjson.jsonarray;import  com.alibaba.fastjson.JSONException;import com.alibaba.fastjson.JSONObject;import  Com.alibaba.fastjson.parser.deserializer.parseprocess;/** * java a field in an object in Jsonarray is sorted  *   *  @author  lijianbo *  @version  1.0 * */public class  Test1 implements parseprocess {public static void main (String[] args)   {string jsonarrstr =  "[ { \" id\ ":  \" 2016-05-25\ ",  \" Name\ ":  \" Fargo  chan\ " },{ \" id\ ":  \" 2016-05-23\ ",  \" name\ ":  \" aaron luke\ " },{  \ "id\":  \ "2016-05-26\",  \ "name\":  \ "dilip singh\" &NBSP;}] "; System.out.println ("Before sorting:" +jsonarrstr); string jsonarraysort =&Nbsp;jsonarraysort (JSONARRSTR); System.out.println ("After sorting:" +jsonarraysort);} /** *  Sort by a field in the object in Jsonarray (with Fastjson)  *  *  @param  jsonarrstr *             json Array String  *  */public  static string jsonarraysort (STRING&NBSP;JSONARRSTR)  {JSONArray jsonArr =  Json.parsearray (JSONARRSTR); Jsonarray sortedjsonarray = new jsonarray (); List<jsonobject> jsonvalues = new arraylist<jsonobject> ();for  (int i  = 0; i < jsonarr.size ();  i++)  {jsonvalues.add (JsonArr.getJSONObject (i)) ;} Collections.sort (jsonvalues, new comparator<jsonobject> ()  {// You can  change  "Name"  with  "ID"  if you want to sort by idprivate  static final String KEY_NAME =  "ID"; @OverridepUblic int compare (jsonobject a, jsonobject b)  {string vala = new  string (); String valb = new string ();try {//  Here is a, B business that needs to be processed and needs to be modified according to your rules. String astr = a.getstring (Key_name); Vala = astr.replaceall ("-",  ""); String bstr = b.getstring (Key_name); Valb = bstr.replaceall ("-",  "");}  catch  (jsonexception e)  {// do something}return -vala.compareto (ValB);//  if you want to change the sort order, simply use the  following:// return -vala.compareto (VALB);}}); for  (Int i = 0; i < jsonarr.size ();  i++)  {sortedJsonArray.add ( Jsonvalues.get (i));} Return sortedjsonarray.tostring ();}}

The result is:

Before sorting: [{"id": "2016-05-25", "name": "Fargo Chan"},{"id": "2016-05-23", "name": "Aaron Luke"},{"id": "2016-05-26", "Nam E ":" Dilip Singh "}] after sorting: [{" Name ":" Dilip Singh "," id ":" 2016-05-26 "},{" name ":" Fargo Chan "," id ":" 2016-05-25 "},{" name ":" Aaron Luke "," ID ":" 2016-05-23 "}]

This method mainly refers to this article:

Http://www.codes51.com/wd/582292.html

This article is from the "Jianbo" blog, make sure to keep this source http://jianboli.blog.51cto.com/12075002/1983017

Sort a field of an object in Jsonarray in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.