Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6225257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:48:36+00:00 2026-05-24T08:48:36+00:00

I have a servlet which talks with the database then returns a list of

  • 0

I have a servlet which talks with the database then returns a list of ordered (ORDER BY time) objects. At the servlet part, I have

                //access DB, returns a list of User objects, ordered
        ArrayList  users = MySQLDatabaseManager.selectUsers();
                //construct response
        JSONObject jsonResponse = new JSONObject();
        int key = 0;
        for(User user:users){
            log("Retrieve User " + user.toString());
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("name", user.getName());
            jsonObj.put("time", user.getTime());
            jsonResponse.put(key, jsonObj);
            key++;
        }
                //write out
        out.print(jsonResponse);

From the log I can see that the database returns User objects in the correct order.

At the front-end, I have

success: function(jsonObj){
            var json = JSON.parse(jsonObj);
            var id = 0;
            $.each(json,function(i,item) {              
                var time = item.time;               
                var name = item.name;               
                id++;
                $("table#usertable tr:last").after('<tr><td>' + id + '</td><td width="20%">' + time + 
                        '</td><td>' + name + 
                        '</td></tr>');
            });

        },

But the order is changed.

I only noticed this when the returned list has large size (over 130 users).

I have tried to debug using Firebug, the “response tab” in Firebug shows the order of the list is different with the log in the servlet.

Did i do anything wrong?

EDIT: Example

{"0":{"time":"2011-07-18 18:14:28","email":"xxx@gmail.com","origin":"origin-xxx","source":"xxx","target":"xxx","url":"xxx"},
"1":{"time":"2011-07-18 18:29:16","email":"xxx@gmail.com","origin":"xxx","source":"xxx","target":"xxx","url":"xxx"},
"2":

,...,
"143":{"time":"2011-08-09 09:57:27","email":"xxx@gmail.com","origin":"xxx","source":"xxx","target":"xxx","url":"xxx"}

,...,
"134":{"time":"2011-08-05 06:02:57","email":"xxx@gmail.com","origin":"xxx","source":"xxx","target":"xxx","url":"xxx"}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:48:37+00:00Added an answer on May 24, 2026 at 8:48 am

    As JSON objects do not inherently have an order, you should use an array within your JSON object to ensure order. As an example (based on your code):

     jsonObj = 
              { items:
                [ { name: "Stack", time: "..." },
                  { name: "Overflow", time: "..." },
                  { name: "Rocks", time: "..." },
                  ... ] };
    

    This structure will ensure that your objects are inserted in the proper sequence.

    Based on the JSON you have above, you could place the objects into an array and then sort the array.

     var myArray = [];
     var resultArray;
    
     for (var j in jsonObj) {
       myArray.push(j);
     }
    
     myArray = $.sort(myArray, function(a, b) { return parseInt(a) > parseInt(b); });
    
     for (var i = 0; i < myArray.length; i++) {
       resultArray.push(jsonObj[myArray[i]]);
     }
    
     //resultArray is now the elements in your jsonObj, properly sorted;
    

    But maybe that’s more complicated than you are looking for..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java servlet which accepts an image a user uploads to my
I have a servlet which processes a request for a long time. It suppose
So I have a doPost method in an order checkout servlet which checks the
I have a servlet which is bundled as part of my application. I want
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it
I have a servlet which takes us to an existing jsp, say home.jsp. This
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
I have a servlet S which handles callbacks from a 3rd party site. The
I'm programming using GWT, which includes Jetty. I have defined my own servlet and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.