Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,562 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,185 people online right now. Registration is fast and FREE... Join Now!




Sort a string array based on results of numeric array sort

 
Reply to this topicStart new topic

Sort a string array based on results of numeric array sort

Bob20000
post 9 Oct, 2008 - 11:38 AM
Post #1


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 4


My Contributions


Hi;
I've been banging my head on this all day.
2 arrays. One contains numeric values, the other contains string values.
I'm trying to sort the numeric array (numerically) and have the new element order of the numeric array similarly reflected in the string array. If this occurs, trace(ss2) would read "A big banana contains a whole bunch of potassium".
I'd prefer not to use an associative array to accomplish this.
Thanks for any input.
Bob
CODE

var ss1:Array = new Array;
var ss2:Array = new Array;

var a1 = 121
var a2 = 3  
var a3 = 1
var a4 = 6
var a5 = 1
var a6 = 8
var a7 = 10
var a8 = 2
var a9 = 14
var a10 = 7

var b1 = "potassium"
var b2 = "banana"
var b3 = "A"
var b4 = "contains"
var b5 = "big"
var b6 = "whole"
var b7 = "bunch"
var b8 = "yellow"
var b9 = "of"
var b10 = "a"

ss1 = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
ss2 = [b1, b2, b3, b4, b5, b6, b7, b8, b9, b10];

trace(ss1);
trace(ss2);

ss2.sort(ss.sort(Array.NUMERIC));


trace(ss1);
trace(ss2);
User is offlineProfile CardPM

Go to the top of the page


DilutedImage
post 9 Oct, 2008 - 08:42 PM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


That sounds like a fairly nonsensical thing to do. Is there a method to the madness?

Perhaps both arrays could use the same key names, so that you could sort the numeric array, and then walk through it and cross-reference the key names against the other array.

You may want to read up on the sort() method first though, because you're using it wrong.

User is offlineProfile CardPM

Go to the top of the page

Bob20000
post 10 Oct, 2008 - 08:45 AM
Post #3


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 4


My Contributions


QUOTE(DilutedImage @ 9 Oct, 2008 - 09:42 PM) *

That sounds like a fairly nonsensical thing to do. Is there a method to the madness?

Perhaps both arrays could use the same key names, so that you could sort the numeric array, and then walk through it and cross-reference the key names against the other array.

You may want to read up on the sort() method first though, because you're using it wrong.





Thanks for the reply;

My method: I'm trying to put together a SRS (spaced repetition sequence) flash card program. It's also known as a Leitner card file system. Briefly, I'm using the Date class to record the time the cards would be played. That would be the numerical array. The words played are the string arrays. As I'm limiting the number of cards played at one time to 50, it makes it easier for me to splice and push the arrays if
I could have all the ones just played sorted out so they are contained at the beginning of the array.
It's probably not the only answer. Just the first one to pop into my head.
I'll go over the sort method some more.
Could you clarify what you meant by "key name"?
Thanks
Bob20000
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 10 Oct, 2008 - 04:59 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


This will require you to change things around a little bit compared to how you are doing them now, but it works:
CODE
var myArray:Array = new Array({word: "potassium", id: 121}, {word: "banana", id: 3}, {word: "A", id: 1},
                              {word: "contains", id: 6}, {word: "big", id: 1}, {word: "whole", id: 8},
                              {word: "bunch", id: 10}, {word: "yellow", id: 2}, {word: "of", id: 14},
                              {word: "a", id: 7});

myArray.sortOn("id", 16);
for(var i:Number = 0; i<10; i++){
    trace(myArray[i].word);
}


Output:
QUOTE
A
big
yellow
banana
contains
a
whole
bunch
of
potassium


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Bob20000
post 11 Oct, 2008 - 08:03 PM
Post #5


New D.I.C Head

*
Joined: 9 Oct, 2008
Posts: 4


My Contributions


Thanks for the suggestion BetaWar.

The solution doesn't work for my project.
I know it's difficult to see the bicycle when all I'm showing is a single spoke.
I think I found a workaround.
Thanks again,
Bob2000
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 02:59AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month