Solved

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

Posted on 2007-11-26
3
158 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now