Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-02-2012, 05:50 PM   #1
macdonjo
Registered User
 
Join Date: May 2011
Posts: 42
Default Simple Array.sort() bug?

ActionScript Code:
var cdCollection:Array = ["Tuskegee","Love Is A Four Letter Word","21","California 37","Up All Night"]; var cdSorted:Array = new Array(); cdSorted = cdCollection; cdSorted.sort(); trace(cdCollection); trace(cdSorted);

Why is cdCollection sorted? I don't want it sorted.
macdonjo is offline   Reply With Quote
Old 05-02-2012, 06:03 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

It's of course not a bug at all. It's just that you don't know how to work with object types and references. When you declare an array, storing that array object in different references doesn't create copies of that array but just points to the same array constantly. In your code cdCollection is not sorted at all or even cdSorted for that matter because those are only variables, references to an array object. What is sorted is an array object that your two variables, cdSorted and cdCollection point to. Calling sort on cdCollection or cdSorted does the same thing cos they both point to the same array.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 05-02-2012, 06:05 PM   #3
macdonjo
Registered User
 
Join Date: May 2011
Posts: 42
Default

Ok, so how do I do what I wanted?
I want cdSorted to be sorted and cdCollection to be left untouched.
macdonjo is offline   Reply With Quote
Old 05-02-2012, 06:08 PM   #4
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Quote:
Originally Posted by macdonjo View Post
I want cdSorted to be sorted and cdCollection to be left untouched.
To do that you need to work with another array so that the first one is not touched by whatever you do with the second one. There's an easy way to duplicate an array which creates a copy of an existing one:
ActionScript Code:
var cdCollection:Array = ["Tuskegee","Love Is A Four Letter Word","21","California 37","Up All Night"]; var cdSorted:Array; cdSorted = cdCollection.concat();//concat without arguments creates a copy cdSorted.sort(); trace(cdCollection); trace(cdSorted);
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 05-02-2012, 06:19 PM   #5
macdonjo
Registered User
 
Join Date: May 2011
Posts: 42
Default

Thanks, I found the code.. copiedArray = myArray.slice();
And that seems to be working great
Thanks for your help
macdonjo is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:01 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.