Jsonarry sort Java

Source: Internet
Author: User

Class1 Set method If the comparison fields are the same will help you to repeat
public class Datacomparator implements comparator<jsonobject> {

@Override
public int Compare (Jsonobject O1, Jsonobject O2) {
if (! ( (O1 instanceof Jsonobject) && (O2 instanceof Jsonobject)) {
throw new RuntimeException ("Sorry");
}

int num =o2.getstring ("User_count"). CompareTo (O1.getstring ("User_count"));
if (Num ==0)
Return o2.getstring ("user_name"). CompareTo (o1.getstring ("user_name"));
return num;
}
}


Class2
Import java.io.IOException;
Import Java.net.URLDecoder;
Import java.util.Collections;
Import Java.util.Comparator;
Import Java.util.Set;
Import Java.util.TreeSet;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.log4j.Logger;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;

Import Com.mongodb.BasicDBObject;

/**
* List
* @author K
* date:2016.1.23
*/
public class test_management{

/**
*
*/
Private static final long serialversionuid = 305141227327561761L;
private static final Logger Logger = Logger.getlogger (Test_management.class);

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Logger.info ("View");
Request.setcharacterencoding ("UTF-8");

StringBuilder sb = new StringBuilder ();
SB = Getstringbuffer (jar);
}

/**
*
* @param Jsonarray
* @return StringBuffer SB
*/
Private Jsonarray Getstringbuffer (Jsonarray jar) {
StringBuilder sb = new StringBuilder ();
Jsonarray Appendjar = new Jsonarray ();
Set<jsonobject> list=new treeset<> (New Datacomparator ());
Assemble the required data
for (int i = 0; I<jar.size (); i++) {
Jsonobject jsob = Jar.getjsonobject (i);
Jsonobject appendjsob = new Jsonobject ();
Appendjsob.put ("id", id);//id
Appendjsob.put ("user_name", user_name);//user name
Appendjsob.put ("province", province));//Province
Appendjsob.put ("city");//Cities
Appendjsob.put ("Department_count", department_count);//Department
Appendjsob.put ("Room_count", room_count);//Number of rooms
Appendjsob.put ("User_count", user_count);//Number of users
Appendjar.add (APPENDJSOB);

}

List.addall (Appendjar);//Start sort operation
Appendjar = Jsonarray.fromobject (list), converted to JSON array
return Appendjar;
}

}

Jsonarry sort 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.