0

I have a JSON loop and I am able to get objects in the loop, but the problem is my output is not in order:

My expected output is:

Menu Id 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23

but I get results like:

Menu Id: 19,18,23,16,17,14,15,12,13,21,20,22,4,3,11,2,1.

How can I sort this the way I expect?

           JSONArray values = menuObject.toJSONArray(names);
           for (int i = 0; i< values.length(); i++) {
                        JSONObject json2 = (JSONObject) values.getJSONObject(i);
                        //int menu_id = json2.getInt("menu_id");
                        menu_id = json2.getString("menu_id");
                        int m_id = Integer.parseInt(menu_id);
                        if (json2.has("menu_parent")) {
                           menu_parent = json2.getString("menu_parent");    
                        }
            if (m_id < 0) {

               //
             } else {
               id = id + menu_id + ",";
               int menu_category = Integer.parseInt(menu_parent);
               System.out.println("Menu Id" + id);
                   if (json2.has("menu_name")) {
                     menu_list = json2.getString(KEY_MENU).trim();
                     System.out.println("Menu List" +menu_title);
                     menu_title = menu_title + menu_list + ",";
                   }
               }

           }
3
  • 1
    the json does not guarantee the order Commented Jan 30, 2013 at 13:25
  • @blackbelt Why it is so.... Commented Jan 30, 2013 at 13:29
  • 1
    @user1051599 it is the definition of json: An object is an unordered set of name/value pairs Commented Jan 30, 2013 at 14:32

2 Answers 2

1

I think you can get it sorted this way:

Collections.sort("your data", new Comparator<Item>() {
        public int compare(Item i1, Item i2) {
            return i1.getCaption().compareTo(i2.getCaption());
        }
    });

After this is done, it should be sorted out.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

String menuids = "19,18,23,16,17,14,15,12,13,21,20,22,4,3,11,2,1";
        String[] ids = menuids.split(",");

                Integer[] result = new Integer[ids.length];
                for (int i = 0; i < ids.length; i++) {
                    result[i] = Integer.parseInt(ids[i].trim());
                }


                Collections.sort(Arrays.asList(result));

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.length; i++) {
                    Integer intValue = result[i];
                    sb.append(intValue);
                    if (i < result.length - 1) {
                        sb.append(", ");
                    }
                }

        String finaldata = sb.toString();

        System.out.println(finaldata);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.