1

I have a custom rest action like this (in a class that extends yii/rest/ActiveController):

public function actionTest()
{        
    return ["9" => "Nine", "1" => "one"];
}

When calling the API, the array output is in reverse order, ie:

{
    "1": "One"
    "9": "Nine",
}

I would like to have it in the original (expected) order...

Seems like the array was sorted somewhere after the array was returned in the action, but I can't figure out where. This only happens when the array key is an integer, an array like this is sorted as expected:

["id-9" => "Nine", "id-1" => "one"]

Have tried using an ArrayDataProvider setting 'sort' = false, but that made no difference.

5
  • Looks like you're returning a dictionary, not an array. If you are, then you cannot return an "ordered dictionary", dictionaries usually revolves around a hashmap, which has no order. Is there a "sorted dictionary" type in the language/runtime you're using? Commented Nov 2, 2015 at 8:44
  • The short array syntax [] is the same as 'array()' since PHP 5.4, so it should not matter. Commented Nov 2, 2015 at 10:28
  • I'm only commenting since you have a key-value data structure that is also serialized to json (I assume) as an object. This would explain why it has no order, a dictionary usually has no order because of the way keys are hashed. Still assuming this is json, a json object does not preserve key ordering, this is specifically highlighted in the specification, see stackoverflow.com/questions/7198412/… - If you want to preserve ordering, you must use something other than a dictionary or json object. Commented Nov 2, 2015 at 10:47
  • Why do you need to preserve the order? Commented Nov 2, 2015 at 10:48
  • You are right about it being a Json-issue. I only looked at the API's json-output, and not the raw output that is in the expected order. Thanks! Solved for me. Write your comment as an answer and I will approve it. Commented Nov 2, 2015 at 11:15

1 Answer 1

4

Since you're exporting it as json and looking at that, from this question - Keeping dictionary keys of JSON Object in order in Objective C, the answer left by Henning Makholm says that:

In JSON objects, by definition, the order of the key-value pairs is not meaningful. The specification allows a JSON producer to permute them any way it want, even at random -- and does not require a parser to preserve the ordering. RFC 4627 says:

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

So json has no ordering, since it uses a dictionary as its data structure and typically dictionaries will have no implicit or explicit ordering due to the way they hash the keys for quick access.

It may be that the actual underlying representation in your program is ordered, but the json output has no such guarantee.

One way to fix this would be to move to a different data structure where order is preserved.

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

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.