/    Sign up×
Community /Pin to ProfileBookmark

Help – two dimensional array

If I have a two dimensional array that looks like this:
0,0
1,14.50
2,28.32
3,23.22
4,26.14
5,30.09

How can I reverse the columns independently to look like this?
5,30.09
4,26.14
3,23.22
2,28.32
1,14.50
0,0

reverse() or a “numerical” sort() does not work unless I’m doing it wrong.

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — If you can convert the two dimensional array into a one dimensional array that looks like this:
<i>
</i>var myArray=new Array(
0,0,
1,14.50,
2,28.32,
3,23.22,
4,26.14,
5,30.09
);

Then you can use one of my Prototype functions:

[url=http://www.webdeveloper.com/forum/showthread.php?p=467719#post467719]Array.prototype.keyAndValueSort[/url]

However that prototype function will do [i]almost[/i] what you want.

You'd need to find the statement in that function:
nKeys.sort();
And change it to:
nKeys.sort().reverse();
Then it will do what you want.
<i>
</i>&lt;script type="text/javascript"&gt;
Array.prototype.keyAndValueSort=function(){
var l=this.length;
var bl=l;if(bl%2==1)bl--;

var keys=new Array();
var values=new Array();

for(var i=0;i&lt;l;i+=2){
keys[keys.length]=this[i]
if(i==bl){values[values.length]="";break}
values[values.length]=this[i+1]
}

l=keys.length
var nObj={};
for(i=0;i&lt;l;i++){
nObj[keys[i]]=values[i];
}

var nKeys=new Array();
for(i in nObj){
nKeys[nKeys.length]=i
}
nKeys.sort().reverse();
l=nKeys.length;

var nValues=new Array();
for(i=0;i&lt;l;i++){
nValues[nValues.length]=nObj[nKeys[i]]
}

var outArray=new Array();
for(i=0;i&lt;l;i++){
outArray[outArray.length]=nKeys[i]
outArray[outArray.length]=nValues[i]
}
return outArray;
}
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
var myArray=new Array(
0,0,
1,14.50,
2,28.32,
3,23.22,
4,26.14,
5,30.09
);
alert(myArray);
alert(myArray.keyAndValueSort());
&lt;/script&gt;
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — The way it works is by accepting your array and generating an associative array that will be able to accept a key and return the value that goes with it. That way all you need is that associative array and an array containing only the keys and not the values so you can rearrange the keys however you wish. Then the output of the function uses all the keys in the same order they are listed in the array to look up the value for each key within the associative array and generate a new array containing all the values without the keys in the same order that goes with the keys.

i.e. it would look like this at that step:
<i>
</i>var nKeys=[5,4,3,2,1,0];
var nValues=[30.09,26.14,23.22,28.32,14.50,0];

Finally the array will return the keys and values together in one array using the same format that went into the array:

[5,30.09,4,26.14,3,23.22,2,28.32,1,14.50,0,0]
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — Actually, what is the code for your "two dimensional array"? You can most-probably just use the reverse array prototype function calling it twice once for each array.

<i>
</i>&lt;script type="text/javascript"&gt;
var twoDArray=[[0,1,2,3,4,5],[0,14.50,28.32,23.22,26.14,30.09]];
alert(twoDArray[0].join(",")+"n"+twoDArray[1].join(","))
twoDArray[0].reverse();
twoDArray[1].reverse();
alert(twoDArray[0].join(",")+"n"+twoDArray[1].join(","))
&lt;/script&gt;
Copy linkTweet thisAlerts:
@edp1959authorNov 22.2005 — OK here is part of my code. I'm collecting data from fields on a web page.

var y=document.cMount.T1.value;

do

{

var AR1=new Array(i, pWTL);

// i'm using write() for a visual in test mode only

document.write(AR1 + "<br />")

i++;

x=((lbIN*i) + l);

l=x;

bWT1=(i*
(aa+bb));

pWTL=x.toFixed(4);

pWTR=x.toFixed(4);

e=(bWT1+x);

rSM=e.toFixed(4);

}

while (i <= y);

//So if y = 10 Then my array would look like this

0,0

1,2.0025

2,6.0075

3,12.0150

4,20.0250

5,30.0375

6,42.0525

7,56.0700

8,72.0900

9,90.1125

10,110.1375

I would like to "reverse" this as previously stated.
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — Have you tried a backwards loop?
<i>
</i>var y=document.cMount.T1.value;

var i=y;
do
{
var AR1=new Array(i, pWTL);

// i'm using write() for a visual in test mode only
document.write(AR1 + "&lt;br /&gt;")

i--;
x=((lbIN*i) + l);
l=x;
bWT1=(i*(aa+bb));
pWTL=x.toFixed(4);
pWTR=x.toFixed(4);
e=(bWT1+x);
rSM=e.toFixed(4);
}
while (i &gt;0);
Copy linkTweet thisAlerts:
@edp1959authorNov 22.2005 — it almost worked...only reversed the 1st column (i) "1-10" and not (pWTL).
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — There are too many variables involved and I don't see how your array ever becomes two dimensional.

Do you have more variables like AR2, AR3 or something?
Copy linkTweet thisAlerts:
@edp1959authorNov 22.2005 — yeah you're right I was wrong in calling this two dimensional. I'm new at this as you can tell. What I'm trying to do is have a ID column (1 - y) and a data column. In order for the data to be correct, it has to be calculated in ascending order. I would then like to reverse both the ID and corresponding data together. Hope I'm making sense.
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — Can you give me example int values for the varaibles so I can see how the chart is calcuated?

i,pWTL,pWTR,lbIN,l,aa,bb,e,bWT1
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — If you only care about the document.write, you can put it into an actual 2d array:
<i>
</i>&lt;script type="text/javascript"&gt;
var wA=new Array();
var i=0;
var y=10
pWTL=0
pWTR=0
lbIN=1.75
l=0
aa=34
bb=435
bWT1=3
e=435
do
{
var AR1=new Array(i, pWTL);

// i'm using write() for a visual in test mode only
wA.push(AR1)

i++;
x=((lbIN*i) + l);
l=x;
bWT1=(i*(aa+bb));
pWTL=x.toFixed(4);
pWTR=x.toFixed(4);
e=(bWT1+x);
rSM=e.toFixed(4);
}
while (i &lt;= y);

wA.reverse();
for(j in wA){
document.write(wA[j]+"&lt;br&gt;")

}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@edp1959authorNov 22.2005 — Let's clean this up a little the only data I want is pWTL.

var i = 0

var lbIN = 2.646

var l = 0

var y=10

var i=y;

do

{

var AR1=new Array(i, pWTL);

// i'm using write() for a visual in test mode only

document.write(AR1 + "<br />")

i--;

x=((lbIN*i) + l);

l=x;

pWTL=x.toFixed(4);

}

while (i >0);
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — I don't understand why the only data you want is pWTL while you are using a document.write for a visual test. In order for a backwards loop to work, you must know the final value for pWTL ahead of time. Just use my approach in post #11
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — Forget about the backwards loop. The method I used in post #11 used again in your new cleaned up example:
<i>
</i>&lt;script type="text/javascript"&gt;

var i = 0
var lbIN = 2.646
var l = 0
var y=10
var pWTL=0
var infoArray=new Array();
do
{
var AR1=new Array(i, pWTL);

// i'm using write() for a visual in test mode only
infoArray.push(AR1)

i++;
x=((lbIN*i) + l);
l=x;
pWTL=x.toFixed(4);
}
while (i &lt;=y);
infoArray.reverse();
for(j in infoArray){
document.write(infoArray[j]+"&lt;br&gt;")
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@edp1959authorNov 22.2005 — worked great thanks for your help.
Copy linkTweet thisAlerts:
@UltimaterNov 22.2005 — Great! Happy to be of help!
×

Success!

Help @edp1959 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...