Posted by: prajapatinilesh | June 10, 2010

Javascript + Array sort on Key

<HTML>
<HEAD>
<TITLE> array key sort </TITLE>
<script language=”javascript”>
jsArray = new Array();
jsArray[‘A’] = ‘Test1’;
jsArray[‘Z’] = ‘Test3’;
jsArray[‘P’] = ‘Test4’;
jsArray[‘U’] = ‘Test2’;
jsArray[‘B’] = ‘Test5’;

array = jsArray;
var keys = new Array();
for(k in array)
{
keys.push(k);
}

keys.sort( function (a, b){return (a > b) – (a < b);} );

for (var i = 0; i < keys.length; i++)
{
alert(keys[i] + ” , ” + array[keys[i]]);
}
</script>
</HEAD>

<BODY>
Javascript + Array sort on Key
</BODY>
</HTML>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: