Solved

array.sort() being ignored in a for... in loop

Posted on 2007-11-26
3
163 Views
Last Modified: 2010-04-03
I have an array whihc I have put a sort on, when I sue a for in loop to loop through the array, it acts as if the array hadn't been sorted. I noticed that using a regular for loop solves this problem, however, however I need this to work with the for in loop. I am aware that a for in will go through an array in reverse order, however this isn't the issue.

Here is the code greatly simplified code, and below, the results of the traces

Many thanks for helping solve this problem

function layout() {
      var cats = ["40artist", "30still", "50fashion"];
      trace("---- Part 1: "+cats);
      cats.sort();
      trace("---- Part 2: "+cats+"\n");
      for (var i in cats) {
            trace("---- Part 3: "+cats[i]);
      }
      trace("\n---- Part 4: "+cats+"\n");
}

layout();


---- Part 1: 40artist,30still,50fashion
---- Part 2: 30still,40artist,50fashion

---- Part 3: 40artist
---- Part 3: 30still
---- Part 3: 50fashion

---- Part 4: 30still,40artist,50fashion

0
Comment
Question by:cursive
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20355861
Try changing, though your code seems working for me.
cats.sort();
to
cats = cats.sort();
0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 20356084
cats.sort() is working fine in your code,
problem is that you are tracing the part3 using "for in" loop which does not gets effected with Array.sort();
you should use "for()" loop

here is the fixed code and it works fine:

function layout()
{
	var cats = ["40artist", "30still", "50fashion"];
	trace("---- Part 1: "+cats);
	cats.sort();
	trace("---- Part 2: "+cats+"\n");
	for (var i = 0; i<cats.length; i++)
	{
		trace("---- Part 3: "+cats[i]);
	}
	trace("\n---- Part 4: "+cats+"\n");
}
 
layout();

Open in new window

0
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 20356105
If you have to have "for in" sorting then you can do it with using an extra temp Array

here is the code which will do the trick for you
function layout()
{
	var cats = ["40artist", "30still", "50fashion"];
	trace("---- Part 1: "+cats);
	cats.sort();
	trace("---- Part 2: "+cats+"\n");
 
	var tmp_arr = [];
	for (var i = 0; i<cats.length; i++)
	{
		tmp_arr.push(cats[i]);
	}
	cats = tmp_arr;
	for (var i in cats)
	{
		trace("---- Part 3: "+cats[i]);
	}
	trace("\n---- Part 4: "+cats+"\n");
}
 
layout();

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question