对jsonArray数组存储的对象做自定义按多个值排序

 public void batchRepayment() throws IOException{

  String jsonString = "[{ \"orderId\": \"431\", \"installment\": \"9\" }, { \"orderId\": \"135\", \"installment\": \"9\" },{ \"orderId\": \"432\", \"installment\": \"9\" },{ \"orderId\": \"135\", \"installment\": \"8\" },{ \"orderId\": \"135\", \"installment\": \"2\" },{ \"orderId\": \"432\", \"installment\": \"5\" },{ \"orderId\": \"431\", \"installment\": \"3\" }]";


        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        if(jsonArray != null){
           List<JSONObject> jsonList = new ArrayList<JSONObject>();
           for (int i = 0; i < jsonArray.size(); i++) {
                jsonList.add(jsonArray.getJSONObject(i));
           }
           //重写排序方法,按照order by orderId,installment方式排序,确保同个订单orderId优先还期数installment靠前的
           Collections.sort( jsonList, new Comparator<JSONObject>() {
               private static final String KEY_NAME = "orderId";
               private static final String KEY_NAME2 = "installment";

               @Override
               public int compare(JSONObject a, JSONObject b) {
                   String valA_1 = new String();
                   String valB_1 = new String();
                   String valA_2 = new String();
                   String valB_2 = new String();

                   try {
                       valA_1 =  a.getString(KEY_NAME);
                       valB_1 =  b.getString(KEY_NAME);
                       valA_2 =  a.getString(KEY_NAME2);
                       valB_2 =  b.getString(KEY_NAME2);
                   }catch (JSONException e) {
                       //do something
                   }
                   int flag = valA_1.compareTo(valB_1);
                   if(flag == 0){
                      return valA_2.compareTo(valB_2);
                   }else{
                      return flag;
                   }
               }

           });

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
cJSON源码,为c语言编写,里面含有测试案例,需要用的自取。可以创建json字符串。 ## Usage ### Welcome to cJSON. cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file. JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state. As a library, cJSON exists to take away as much legwork as it can, but not get in your way. As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it in one of two modes: Auto and Manual. Let's have a quick run-through. I lifted some JSON from this page: http://www.json.org/fatfree.html That page inspired me to write cJSON, which is a parser that tries to share the same philosophy as JSON itself. Simple, dumb, out of the way. ### Building There are several ways to incorporate cJSON into your project. #### copying the source Because the entire library is only one C file and one header file, you can just copy `cJSON.h` and `cJSON.c` to your projects source and start using it. cJSON is written in ANSI C (C89) in order to support as many platforms and compilers as possible. #### CMake With CMake, cJSON supports a full blown build system. This way you get the most features. CMake with an equal or higher version than 2.8.5 is supported. With CMake it is recommended to do an out of tree build, meaning the compiled files are put in a directory separate from the source files. So in order to build cJSON with CMake on a Unix platform, make a `build` directory and run CMake inside it. ``` mkdir build cd build cmake .. ``` This will create a Makefile and a bunch of other files. You can then compile it: ``` make ``` And install it with `make install` if you want. By default it installs the headers `/usr/local/include/cjson` and the libraries to `/usr/local/lib`. It also installs files for pkg-config to make it ea

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值