JSON is sorted by key automatically in Firefox, Chrome and Opera

5 years ago

radas
Member | 210
+
0
-

Hi,
I have model method with this SQL (dibi):

->select(array('id', 'name'))
->from('table')
->orderBy('name', \dibi::ASC)
->fetchPairs()

Presenter:

public function handleLoad() {
  if ($this->isAjax() {
    $data = ...  // call model method
    $this->payload->data = $data;  // array()
    $this->sendPayload();
  }
  ...
}

Firefox (25), Chrome (31) and Opera (12) sort JSON object by key:

Object {
  3 = "Z...",
  4 = "S...",
  5 = "V..."
}

Internet Explorer 8 is ok.

Is possible, that Nette would it correct?

--
Nette 2.1-dev (2013–11–18)

Last edited by radas (2013-11-21 14:19)

5 years ago

Jan Tvrdík
Nette guru | 2544
+
0
-

@radas: JSON RFC: “An object is an unordered collection”. This is also true for javascript. Relying on the order of items in object is wrong.

5 years ago

radas
Member | 210
+
0
-

@Jan Tvrdík: OK, thanks, this is the solution for me.