By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,946 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,946 IT Pros & Developers. It's quick & easy.

for (var i in obj) question

Claus Mygind
100+
P: 571
I have an array of objects.

I display the content of the array in a table.

Then I have a link which lets the user sort the table. I do that by sorting the array, delete the content of the table and then redisplay the content of the array. But the "for (var i in obj)" seems to use the original order. I can see in firebug that the array is properly sorted. So why does it maintain the original sort order?

Here is my sample code and below is a screen shot of the array after sorting;

Expand|Select|Wrap|Line Numbers
  1. function a7DayKeysSort()
  2. {
  3. /*
  4. -----------------------------------------
  5.     clear previous values
  6.         but preserve the header line
  7. -----------------------------------------
  8.     */
  9.     var tr, td
  10.     tbody = document.getElementById("dJobLines");
  11.     while (tbody.rows.length > 1) 
  12.     {
  13.         tbody.deleteRow(1);
  14.     }
  15. /*
  16. --------------------------------
  17.        sort content of array
  18. --------------------------------
  19. */
  20.     a7DayKeys.sort();
  21.  
  22. /*
  23. ----------------------------------------------
  24.        now re-display content of array
  25. ----------------------------------------------
  26. */
  27.     for ( var ix in a7DayKeys )
  28.     {
  29.         cKey = a7DayKeys[ix];
  30.  
  31.         tr = tbody.insertRow(tbody.rows.length);
  32.  
  33.         td = tr.insertCell(tr.cells.length);
  34.         td.setAttribute("align", "center");
  35.         td.innerHTML = cKey.pJob;
  36.  
  37.         td = tr.insertCell(tr.cells.length);
  38.         td.innerHTML = cKey.pName;
  39.  
  40.         td = tr.insertCell(tr.cells.length);
  41.         td.setAttribute("align", "center");
  42.         td.innerHTML = cKey.pPC;
  43.  
  44.         //load check boxes for true values
  45.         for (var nx = 0; nx < 7; nx++ )
  46.         {
  47.             td = tr.insertCell(tr.cells.length);
  48.             td.setAttribute("align", "center");
  49.             if ( cKey[nx] == true )
  50.             {
  51.                 td.innerHTML = '<INPUT TYPE="checkbox" NAME="">';
  52.             }else{
  53.                 td.innerHTML = "&nbsp;";
  54.             }
  55.         }
  56.     }
  57. }
  58.  

Sep 10 '09 #1
Share this Question
Share on Google+
10 Replies


Dormilich
Expert Mod 5K+
P: 8,639
upon trying myself, sorting occured.
Sep 10 '09 #2

Claus Mygind
100+
P: 571
I just added a picture of the array as viewed from firebug. The array is sorted but the "for ( var i in obj)" is using the original sort order. Why would this be?
Sep 10 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
judging from the picture, a7DayKeys is not a standard array (besides that Firebug always display the properties and methods sorted)

out of interest, what does
Expand|Select|Wrap|Line Numbers
  1. typeof a7DayKeys
give?
Sep 10 '09 #4

acoder
Expert Mod 15k+
P: 16,027
What exactly are you sorting on? Except for very simple alphabetical sorts, you will need to supply a sort function to sort() - see https://developer.mozilla.org/en/Cor...cts/Array/sort.
Sep 10 '09 #5

Claus Mygind
100+
P: 571
@Dormilich
It returns that a7DayKeys is an "Object"
Sep 10 '09 #6

Claus Mygind
100+
P: 571
@acoder

I just wanted the keys of the object/array to be displayed in a sorted order. As was previously pointed out the display I show in my picture is the order I wish the records to appear. The unique key is made up of "-" + .job +.pc

initially I left out the "_" from the key. When I did that I was able to sort, but the key was changed from the original value to a standard array 0, 1, 2 etc.

also the key value is not always a number as shown. Other values can start with a letter like P or L.

So I added the underscore to make all values strings. That's when my trouble started.

I have other sorts on the same page where I do employ the suggestion in the link you sent and they work fine. In those arrays I sort on one of the attribute values.

So what you are saying I need to create a function that compares the value of the attributes, or create an attribute with the same value of Job+Pc and use that to compare.

Expand|Select|Wrap|Line Numbers
  1. a7DayKeys.sort(stringAsort);
  2.  
  3. //sorts character fields ascending order
  4. function stringAsort(a, b) {
  5.     a = a[job].toLowerCase();
  6.     b = b[job].toLowerCase();
  7.     return ((a < b) ? -1 : ((a > b) ? 1 : 0));
  8. }
  9.  
Sep 10 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
be aware that Array.sort() works on the values, not on the keys! (and because your values itself are objects again…)

in this regard you should refrain from terming your object as array (though it technically may be an array)
Sep 10 '09 #8

Claus Mygind
100+
P: 571
@Dormilich

Ok I see what you are saying. We will call it an object. Can I sort the attributes of the object? So they are recalled in the order that is shown?

I stated that poorly.

Can I sort the list of objects in my object

Not sure if that makes it clearer
Sep 10 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
can you make it a real array (depends on the usage) like
Expand|Select|Wrap|Line Numbers
  1. var new_entry_obj = new Entry([false, true, false, …], 65583, "OAK BRO", 60);
  2. // the Entry object has a toString() method,
  3. // which will return "712634" (or whatever)
  4. // so that you can apply Array.sort()
  5. a7DayKeys.push(new_entry_obj);
?

you could also ask the FireBug guys ;)
Sep 10 '09 #10

Claus Mygind
100+
P: 571
Thanks for your reply. I think at this time I am going to drop the idea of the sort option. Looks like I will have to study arrays and objects some more.
Sep 10 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.