-1

I need to sort JSON object alphabetically.

I want to send it to array of objects and then sort. Now I was successful in sending it to multidimensional array and sort but still not able to sort array of objects.

I have object.

var Response = { 
    "10001": "Fort Worth",
    "10002": "Dallas",
    "32402": "Austin"         
};

I send it to array.

var sort_array_of_array = [];
for (var my_key in Response) {
    var inner_array = [];
    inner_array[0] = my_key;
    inner_array[1] = Response[my_key];
    sort_array_of_array.push(inner_array);         
}     

In result I get an array

sort_array_of_array: Array[3]
0: Array[2]
    0: "10001"
    1: "Fort Worth"
    length: 2     
1: Array[2]
    0: "10002"
    1: "Dallas"
    length: 2     
2: Array[2]
    length: 3

I sorted it.

sort_array_of_array.sort(function(a, b)
{   
    // if they are equal, return 0 (no sorting)
    if (a[1] == b[1]) { return 0; }
    if (a[1] > b[1])
    {
        // if a should come after b, return 1
        return 1;
    }
    else
    {
        // if b should come after a, return -1
        return -1;
    }
});

I checked the sorted array.

<div id="my_after_sort"></div>

for (i = 0; i < sort_array_of_array.length; ++i) {
    //alert(sort_array_of_array[i][0]);
   $('#my_after_sort').append(sort_array_of_array[i][1]);
}

But if I send the original object to array of objects I fail to sort it.

var sort_array_of_object = [];
for (var key in Response) {
    sort_array_of_object.push({key:Response[key]});         
}

Is it possible to sort this array of objects and how to do it?

фымышонок
  • 1,362
  • 16
  • 22
  • to force php json_decode to convert 'objects' as 'php' array then use: $phpArray = json_decode($jsonString, TRUE);. saves a lot of hassle. p.s. you do not need to use uppercase, was trying to be clear. – Ryan Vincent Apr 17 '14 at 21:16
  • When processing arrays that you you may not know the indexes of. Then use either: 'foreach( $array as $key => $data) { ... Or, use 'ArrayIterator'. Both have there uses. I suggest that you do not try nesting indexes using 'obvious values' unless you are told that they are valid. e.g $data[0][1]. The first may start at [1] and the second at [2]. The suggested methods work fine. Note: PHP 'arrays' **are not arrays** – Ryan Vincent Apr 17 '14 at 21:30
  • I guess it's PHP, but could you add your language tag to your question? – Larme Apr 23 '14 at 14:40
  • possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Paul May 02 '14 at 03:16
  • `Response` is an `Object`. Type `Object` can not be sorted. Type `Array` can be sorted, that is why programmers are directed to transform object into array, or array of array, before sorting. – Paul May 02 '14 at 03:17

1 Answers1

0

It would be much simpler to make an array of objects like so:

var arrayOfObjects = [];
for (var key in Response) {
  arrayOfObjects.push({
    key: key,
    name: Response[key]
  });
}

Then you can sort it by comparing the name property of each element:

arrayOfObjects.sort(function(a,b) {
  if (a.name > b.name) {
    return 1;
  } else if (a.name < b.name) {
    return -1;
  } else {
    return 0;
  }
});
jshanley
  • 9,048
  • 2
  • 37
  • 44