PDA

View Full Version : Sorting an Array..

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>
<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 >
{
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>
<body MS_POSITIONING="GridLayout">
<form id="Form1">
<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.

jkd
08-07-2006, 02:15 AM
myArray.sort(function(a,b) {
return a.split("~")[1] - b.split("~")[1];
});

Where myArray is your array of values, and the [1] represents the second column in your "a~b~c" configuration (the b's). If you wanted the c's, then use [2] instead.

jkd
08-07-2006, 02:19 AM
Oh, as for the ones starting with 0, you'll have to remove those separately. Firefox has a nice Array.prototype.filter method, but no other browser does natively, so you'll just need to iterate through the array and build up a new one manually.

wowrang
08-07-2006, 03:14 AM
Oh, as for the ones starting with 0, you'll have to remove those separately. Firefox has a nice Array.prototype.filter method, but no other browser does natively, so you'll just need to iterate through the array and build up a new one manually.

Thanks alot JKD, Acutally i started writing code, when posted this thread, its comes around 50 lines. your is very simple and efficent :thumbsup: . thx. :)