3

I'm serializing a bunch of objects user Django Rest Framework and retrieving the information with angularjs on the frontend. When I check the response in the browser, my array of objects that get returned have all their fields in alphabetical order rather then the ordering instructions I gave in the backend.

What is forcing this alphabetical ordering? Django Rest Framework or angularjs? This ordering is messing up my table I'm trying to create.

data['objects'] = serializer.data      
print data['objects'][0]               
return Response(data) 

Print output from django

{'hard_weight': 0L, 'current_count': 8020L, 'mileage': 0L, 'route_type': None, 'route': u'NO SCHEDULE', 'destination': None, 'pickup_id
': 657L, 'specials': 900000L, 'soft_weight': 0L, 'misses': 0L, 'date': datetime.date(2030, 12, 31), 'estimated_hours': Decimal('0.00'),
 'estimated_hours_unload': Decimal('0.00'), 'cancel': False}

console.log of returned data:

   cancel: false
    current_count: 8020
    date: "2030-12-31"
    destination: null
    estimated_hours: "0.00"
    estimated_hours_unload: "0.00"
    hard_weight: 0
    mileage: 0
    misses: 0
    pickup_id: 657
    route: "NO SCHEDULE"
    route_type: null
    soft_weight: 0
    specials: 900000

Update/Solution I ended up writing this function to take in each one of my objects and put them in an array in order of a given list.

function convertObjects(objects, list){              
    blank = []                                       
    for(var i=0; i <objects.length; i++)             
    {                                                
        obj = []                                     
        object = objects[i]                          

        for (var j=0; j<=list.length; j ++)  
        {                                            
            obj.push(object[list[j]])        
        };                                           
        blank.push(obj)                              
    };                                               

    return blank                                     
}                  
Austin
  • 4,296
  • 6
  • 40
  • 52

2 Answers2

1

The ordering is not do to either Django or AngularJS, it is happening in the browser due to the fact that JavaScript objects have no specified order. See the ECMAScript specification, particularly section 4.3.3:

4.3.3 object

member of the type Object

NOTE An object is a collection of properties and has a single prototype object. The prototype may be the null value.

Note that it does not specify an order for the collection of properties.

Also relevant is the specification defining object property enumeration in section 12.6.4

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

You will have to customize the order on the client side in AngularJS. However, note that the orderBy filter only works on Arrays. See this answer for creating a custom directive to order object properties:

https://stackoverflow.com/a/18186947/1870541

Community
  • 1
  • 1
Fiver
  • 9,909
  • 9
  • 43
  • 63
1

Ordinarily, the dictionary / map / key-value data structure doesn't store or care about the order of the keys. Two dictionary with the same key-values, but in different orders, are considered identical. You might even say there's no such thing as key order in a dictionary (There are implementation of dictionaries that preserve order though)

If the order in which you display them to the end user is important, e.g. in a UI table, you'll have to get the keys of your object, order them, and then go through your object with each key to get the value you want, http://underscorejs.org/ library has keys and sort utils for you to use

bakkal
  • 54,350
  • 12
  • 131
  • 107