Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorting an Array..

    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.


    Code:
    <!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.
    Last edited by wowrang; 08-07-2006 at 12:47 AM.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Code:
    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.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jkd
    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 . thx.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •