6

Jquery + Rails 4

<script>
 var jsonData = {
  "81404": "Object",
  "81408": "Object",
  "81416": "Object",
  "80387": "Object",
  "73952": "Object",
  "74697": "Object",
  "81411": "Object",
  "74700": "Object"
 };
console.log(jsonData);
</script>

Mozilla Output (Right, and expected)

Object { 81404="Object", 81408="Object", 81416="Object", 80387="Object", 73952="Object", 74697="Object", 81411="Object", 74700="Object"}

Chrome Output (Wrong, ???)

Object {73952: "Object", 74697: "Object", 74700: "Object", 80387: "Object", 81404: "Object", 81408: "Object", 81411: "Object", 81416: "Object"}

How to fix this automatically sorting issue in Chrome any suggestion help,,,

I am using this data for filtering that's order is important.

user3676578
  • 213
  • 1
  • 5
  • 17

1 Answers1

7

Your data is not an array. It has no intrinsic order. They are just properties on an object.

From this Reference

4.3.3 Object
An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function.

Put them in an array property on the JSON object if order is important (or just use an array!).

e.g. something like:

var jsonData = {data: [
    {"81404": "Object"},
    {"81408": "Object"},
    {"81416": "Object"},
    {"80387": "Object"},
    {"73952": "Object"},
    {"74697": "Object"},
    {"81411": "Object"},
    {"74700": "Object"}]
 };
console.log(jsonData);

or for just the list

console.log(jsonData.data);

It would be helpful to explain what you are doing with the data, so that any example is more applicable.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • How can i change mine jsonData to your's jsonData (old(mine one) to new (yourone)). – user3676578 Jun 16 '14 at 10:53
  • 1
    @user3676578: You need to show how you are building your JSON. At the moment you have only provided a hard-wired example, so got back an equivalent hard-wired example :) – iCollect.it Ltd Jun 16 '14 at 10:55
  • Actually i am working on Ruby on Rails for rails template i am passing this jsonData variable to javascript function there i will do all the filtering of data (order important), for this i am using this . – user3676578 Jun 16 '14 at 11:01
  • 1
    I'm not a Ruby on Rails coder, but if you show the entire problem, tagged as Ruby-on-Rails too. you May get a complete solution. I was only addressing the question you asked :) – iCollect.it Ltd Jun 16 '14 at 11:03
  • @user3676578 - I reckon you will need to use an array instead of a hash in Ruby, too. I even wonder whether Ruby hashes guarantee an order in the items. – chiccodoro Jun 16 '14 at 11:07
  • Yes TrueBlueA you are right, but this is not Ruby on Rails problem Right it is related JS/Jquery/Browser Right, that's why, :) – user3676578 Jun 16 '14 at 11:07
  • @chiccodoro my full functionality depends on this Hash so i cant change Hash to Array, because it will be lot of Rework... – user3676578 Jun 16 '14 at 11:10
  • @TrueBlueAussie how can i loop mine jsonData to get yours jsonData format..at client side only... – user3676578 Jun 16 '14 at 11:12
  • @user3676578: Your object has no order, so you cannot convert it to my version as the order is already lost. You must do this where you get the data. – iCollect.it Ltd Jun 16 '14 at 11:19
  • @user3676578 - either a Ruby hash preserves the order or not. If it does, nothing stops you from creating a new array and filling it your hash right before you generate the json from it. If it does not, then you need to rework the design of your code anyway. – chiccodoro Jun 16 '14 at 12:47
  • 1
    @user3676578 this clearly answers your question and should be marked as the answer. After reading your comments, it is clear that the question you asked did not include enough information to solve your actual problem. Since there is a huge gap between what you asked and what your problem is, you should open a new question for any other issues you are having - after doing your own research, of course. I'm sure you will receive a solution if you involve all the parts of your problem in your question. – lightswitch05 Jun 16 '14 at 15:01
  • I am running into this issue as well...and reading the answer chosen answer above it states It is an unordered collection of properties` - then WHY Is Chrome and Javascript always ordering it?! And if chrome and javascript is always auto-ordering it, then there must be a way of stopping it. My object is unordered, but chrome console is sorted and AngularJS is also auto-sorting it - as I can see in my ng-repeat on a – rolinger Feb 27 '19 at 01:59