wowrang
08-06-2006, 11:42 PM
Hi,
As i am new to javascript, i need help to solve this problem.
I have an array and its value looks like below.
Arr[0]="1~1~5";
Arr[1]="1~2~100";
Arr[2]="1~3~22";
Arr[3]="1~4~1";
Arr[4]="1~5~32";
Arr[5]="0~6~2";
I need output as below.
Arr[0]="1~1~1";
Arr[1]="1~2~5";
Arr[2]="1~3~22";
Arr[3]="1~4~32";
Arr[4]="1~5~100";
or
Arr[0]="1~4~1";
Arr[1]="1~1~5";
Arr[2]="1~3~22";
Arr[3]="1~5~32";
Arr[4]="1~2~100";
Arr[5]="0~6~2";
i dont need Arr[5] because it starts with 0. i need to sort the value which only starts with 1.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script >
function addtoarray()
{
var Arr = new Array();
Arr[0]="1~1~5";
Arr[1]="1~2~100";
Arr[2]="1~3~22";
Arr[3]="1~4~1";
Arr[4]="1~5~32";
Arr[5]="0~6~2";
var str;
for(i = 0; i < Arr.length;i++)
{
if(i==0)
{
str = Arr[0];
}
else
str = str + "^" +Arr[i];
}
document.getElementById("hdnVal2").value = str;
var first = str.split("^");
for(i=0;i< first.length;i++)
{
if(i==0)
{
text = first[0];
}
else
text = text + "~" + first[i];
}
document.getElementById("hdnVal3").value=text;
var newArr = text.split("~");
var temp;
for(i=2;i< newArr.length;i+=3)
{
if(newArr[i]>newArr[i+3])
{
temp = newArr[i];
newArr[i]=newArr[i+3]
newArr[i+3]=temp;
}
}
for(i=0;i<newArr.length;i++)
{
if(i==0)
{
str = newArr[0]
}
else
str = str + "~" +newArr[i];
}
document.getElementById("hdnVal4").value = str;
}
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1">
<input type="button" id="Add" onclick="addtoarray()" />
<input type="text" id="hdnVal2" value="" size=50 /> <br>
<input type="text" id="hdnVal3" value="" size=50/><br>
<input type="text" id="hdnVal4" value="" size=50 />
</form>
</body>
</html>
I created above code, but its not working properly.
Thanks alot.
wowrang.
As i am new to javascript, i need help to solve this problem.
I have an array and its value looks like below.
Arr[0]="1~1~5";
Arr[1]="1~2~100";
Arr[2]="1~3~22";
Arr[3]="1~4~1";
Arr[4]="1~5~32";
Arr[5]="0~6~2";
I need output as below.
Arr[0]="1~1~1";
Arr[1]="1~2~5";
Arr[2]="1~3~22";
Arr[3]="1~4~32";
Arr[4]="1~5~100";
or
Arr[0]="1~4~1";
Arr[1]="1~1~5";
Arr[2]="1~3~22";
Arr[3]="1~5~32";
Arr[4]="1~2~100";
Arr[5]="0~6~2";
i dont need Arr[5] because it starts with 0. i need to sort the value which only starts with 1.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script >
function addtoarray()
{
var Arr = new Array();
Arr[0]="1~1~5";
Arr[1]="1~2~100";
Arr[2]="1~3~22";
Arr[3]="1~4~1";
Arr[4]="1~5~32";
Arr[5]="0~6~2";
var str;
for(i = 0; i < Arr.length;i++)
{
if(i==0)
{
str = Arr[0];
}
else
str = str + "^" +Arr[i];
}
document.getElementById("hdnVal2").value = str;
var first = str.split("^");
for(i=0;i< first.length;i++)
{
if(i==0)
{
text = first[0];
}
else
text = text + "~" + first[i];
}
document.getElementById("hdnVal3").value=text;
var newArr = text.split("~");
var temp;
for(i=2;i< newArr.length;i+=3)
{
if(newArr[i]>newArr[i+3])
{
temp = newArr[i];
newArr[i]=newArr[i+3]
newArr[i+3]=temp;
}
}
for(i=0;i<newArr.length;i++)
{
if(i==0)
{
str = newArr[0]
}
else
str = str + "~" +newArr[i];
}
document.getElementById("hdnVal4").value = str;
}
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1">
<input type="button" id="Add" onclick="addtoarray()" />
<input type="text" id="hdnVal2" value="" size=50 /> <br>
<input type="text" id="hdnVal3" value="" size=50/><br>
<input type="text" id="hdnVal4" value="" size=50 />
</form>
</body>
</html>
I created above code, but its not working properly.
Thanks alot.
wowrang.