Home arrow JavaScript arrow Page 4 - More on JavaScript Array Objects
JAVASCRIPT

More on JavaScript Array Objects


Thanks for dropping by for another episode of our series on JavaScript Array Objects. We left off discussing five of the thirteen array objects JavaScript has to offer. Here, in this issue, we will make an effort to cover the remaining eight.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
December 16, 2008
TABLE OF CONTENTS:
  1. · More on JavaScript Array Objects
  2. · Shift() It Around
  3. · Slice Them Taters
  4. · Sort() It Out
  5. · Fixing the Issue

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
More on JavaScript Array Objects - Sort() It Out
(Page 4 of 5 )

The sort() object is one those guys that don't need an introduction; its name says it all. But for those of you who are really slow, it sorts the elements in an array. The tricky part of this little fellow is that unless you create a function, it sorts numbers alphabetically. Yeah, my head exploded too. I'll show you how to fix that though. First however, we will use it to sort an array of letters:


<html>

<body>

<script type="text/javascript">

var abet = new Array(6);

abet[0] = "c";

abet[1] = "d";

abet[2] = "x";

abet[3] = "r";

abet[4] = "a";

abet[5] = "b";

document.write("Unsorted: ");

document.write(abet + "<br />" + "<br />");

document.write("Sorted: ");

document.write(abet.sort());

</script>

</body>

</html>

The result of this studious code is:

  Unsorted: c,d,x,r,a,b

  Sorted: a,b,c,d,r,x

This works the same way for words, as in this example:


<html>

<body>

<script type="text/javascript">

var abet = new Array(6);

abet[0] = "hamburger";

abet[1] = "hotdog";

abet[2] = "apple";

abet[3] = "carrot";

abet[4] = "juice";

abet[5] = "boar";

document.write("Unsorted: ");

document.write(abet + "<br />" + "<br />");

document.write("Sorted: ");

document.write(abet.sort());

</script>

</body>

</html>

Resulting in:

  Unsorted: hamburger,hotdog,apple,carrot,juice,boar

  Sorted: apple,boar,carrot,hamburger,hotdog,juice

And here is the issue with numbers. If we code it this way:


<html>

<body>

<script type="text/javascript">

var numby = new Array(7);

numby[0] = "100";

numby[1] = "9";

numby[2] = "20";

numby[3] = "25";

numby[4] = "5000";

numby[5] = "40";

numby[6] = "-40";

document.write("Unsorted: ");

document.write(numby + "<br />");

document.write("Sorted: ");

document.write(numby.sort());

</script>

</body>

</html>

This gives us these erroneous results:

  Unsorted: 100,9,20,25,5000,40,-40
  Sorted: -40,100,20,25,40,5000,9


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials