Online
 
Sunday, 23 November 2008
 
 

JavaScript - Binary Sort for Strings | Print |  E-Mail
 
The sort() function recursively sorts an array of strings into alphabetical order. It makes extensive use of the compareTo() function above.
function sort( s )

{

if( s.length == 1 ) return;

else

{

var a = new Array( parseInt( s.length / 2 ) );

var b = new Array( s.length - a.length );

 

for( i = 0 ; i < a.length ; i++ ) a[i] = s[i];

for( i = 0 ; i < b.length ; i++ ) b[i] = s[i+a.length];
sort( a );
sort( b );
var ai = 0, bi = 0, i = 0;
while( ai < a.length || bi < b.length )
{
if( bi >= b.length ) s[i++] = a[ai++];
else if( ai >= a.length ) s[i++] = b[bi++];
else if( a[ai].compareTo( b[bi] ) <= 0 ) s[i++] = a[ai++];
else if( a[ai].compareTo( b[bi] ) > 0 ) s[i++] = b[bi++];
}
}
}

This is an extremely useful function, not only for sorting strings, but also for sorting the items in a list form element.

Following is an example of using this function with an array of strings:

<head>
<title>
JavaScript Professional Projects - Example Function
</title>



<script language="JavaScript">

<!--

var months = new Array( "January", "February", "March",

"April", "June", "July",

"August", "September", "October",

"November", "December" );



function display()

{

for( i = 0 ; i < this.length ; i++ )

{

document.write( this[i] + "<br>" );

}

}

Array.prototype.display = display;



function sort( s )

{

if( s.length == 1 ) return;

else

{

var a = new Array( parseInt( s.length / 2 ) );

var b = new Array( s.length - a.length );



for( i = 0 ; i < a.length ; i++ ) a[i] = s[i];

for( i = 0 ; i < b.length ; i++ ) b[i] = s[i+a.length];

sort( a ); sort( b );



var ai = 0, bi = 0, i = 0;

while( ai < a.length || bi < b.length )

{

if( bi >= b.length ) s[i++] = a[ai++];

else if( ai >= a.length ) s[i++] = b[bi++];

else if( a[ai].compareTo( b[bi] ) <= 0 ) s[i++] = a[ai++];

else if( a[ai].compareTo( b[bi] ) > 0 ) s[i++] = b[bi++];

}

}

}
function compareTo( s )
{
var len1 = this.length;
var len2 = s.length;
var n = ( len1 < len2 ? len1 : len2 );
for( i = 0 ; i < n ; i++ )
{
var a = this.charCodeAt( i );
var b = s.charCodeAt( i )
if( a != b )
{
return( a - b );
}
return( len1 - len2 ); }
String.prototype.compareTo = compareTo;
// -->
</script>
</head>
<body>
<table border=1 cellspacing="2" cellpadding="20">
<tr>
<td width=150><b>Unsorted</b></td>
<td width="150"><b>Sorted</b></td>
</tr>
<tr>
<td>
<script language="JavaScript">
<!--
months.display();
-->
</script>
&nbsp;</td>
<td>
<script language="JavaScript">
<!--
sort( months );
months.display();
-->
</script>
&nbsp;</td>
</tr>
</table>
</body>
</html>

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: javascript, java script, html, free code, window, menu, form, popup, source, date calculator, calendar, javascripts, image, javascript help, javascript popup window, forms, calendars, games, Web design builder, webmaster, development.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Rokok Kretek Jumbo Coklat

“Terbuat dari tembakau, saos dan cengkeh pilihan kwalitas tinggi sehingga menghasilkan rokok dengan rasa yang cocok untuk segala cuaca”
Jumbo Coklat - Powered By G-Ads

 
Top! Top!