0

I have a JSONArray with 5 different JSONObject and each of the 5 JSONObject has an identifier string value. The five values are "aa","bb","erer","cc","gg". My requirement is to get the JSONObject with identifier "erer" to first place and the following JSONObjects can be in any order. The json is:

{
"obj":[
{"identifier":"aa",},
{"identifier":"bb",},
{"identifier":"erer",},
{"identifier":"cc",},
{"identifier":"gg",}
]
}

The final result has to be "erer","aa","bb","cc","gg" and I need to do this in a single loop.

I'm able to do this in two loops.

for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject j = jsonArray.getJSONObject(i); 
     if(j.getString("identifier").equals("erer")) {
     sortedJson.put(joPayLoad);
}
}
for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject j = jsonArray.getJSONObject(i); 
     if(!j.getString("identifier").equals("erer")) {
     sortedJson.put(joPayLoad);
}
}

But the JsonArrray might also have 10,000 JSONObjects. And so, this 'two for-loops' will cause a performance lag. So, please help me to achieve my above said requirement in a single loop. Thanks in advance!

14
  • 1
    I guess you could go for two lists, one where you put all the erer-Objects and one with the rest and then simply concat them after running through the for-loop once. If you use Java you could also use addFirst or addLast depending on the object you are handling. Have a look at the documentation for these methods Commented Jun 22, 2019 at 19:24
  • what type is sortedJson and jsonArray? Commented Jun 22, 2019 at 19:25
  • 2
    It's not mandatory to sort the other jsonobjects. My requirement is to get "erer" to first place and the following elements can be like "gg","aa","cc","bb". It doesn't matter. Commented Jun 22, 2019 at 19:40
  • 1
    Only one "erer" Commented Jun 22, 2019 at 19:41
  • 1
    I apologize, GhostCat. No excuses. Thanks a lot for your time. Commented Jun 22, 2019 at 19:48

2 Answers 2

1

Given JsonArray is a List (source) you can just try to swap the value with erer to be the new first element:

for (int i = 0; i < jsonArray.length(); i++) {
     JsonValue j = jsonArray.get(i); 
     if(j.getString("identifier").equals("erer")) {
        JsonValue tmp = jsonArray.get(0);
        jsonArray.set(0, j);
        jsonArray.set(i, tmp);
        break;
     }
}

Haven't tried it since I don't have EE installed, but maybe you can make it work with the methods provided.

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

2 Comments

This isn't the same as his two-loop example, which can handle any number of "erer" elements.
true, but from the question it sounds like there is only one and that should be the first element.
1

You need two pointers, a read pointer and a write pointer. Both start at the last item. Read the element at the read pointer. If it's "erer," then just decrement the read pointer, otherwise, write the element to the write pointer and decrease both the read pointer and the write pointer. At some point the read pointer will decrease to zero. If the write pointer is >0 it means you found some "erer" elements that you did not write. Write them to the write pointer and decrement the write pointer until it too is zero.

String[] data = { "aa", "gg", "dd", "ee", "erer", "gg", "erer" };

int r = data.length - 1, w = data.length - 1;
while (r >= 0) {
    if (!"erer".equals(data[r])) {
        data[w] = data[r];
        w--;
    }
    r--;
}
while (w >= 0) {
    data[w] = "erer";
    w--;
}

Okay, technically there's still two loops here, but it should be clear that we're iterating only once over the array. If you really gotta have one loop, this would work...

int r = data.length - 1, w = data.length - 1;
while (w >= 0) {
    if (r >= 0) {
        if (!"erer".equals(data[r])) {
            data[w--] = data[r];
        }
        r--;
    } else {
        data[w--] = "erer";
    }
}

3 Comments

This question is tagged Java, not JavaScript.
You're right! That's funny, I wrote it in Java and actually converted into JS because I thought that's what he was using.
Also, I'm "sorting" strings, not JSONObject instances. But the point is to illustrate the loop. The OP knows how to use JSONObject.

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.