Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JavaScript array sort numerically 
 

As in our previous example of sorting array elements alphabetically we have used the default sort method to sort array elements but here in this example of sorting array elements ( numeric elements ).

 

JavaScript array sort numerically

                         

As in our previous example of sorting array elements alphabetically we have used the default sort method to sort array elements but here in this example of sorting array elements ( numeric elements ) we have created a sortNumbers() function which sorts the numbers.

In this example we can see that if we use the default sort() method on the numbers it doesn't sort these numbers correctly. Since it takes these numeric elements as alphabet and therefore we need to define a sorting function for sorting numbers. Given below is the full example code of this example html page:

 

 

 

<html>
<head>
<title>
JavaScript array sort() numerically
</title>
<script type="text/javascript">
var arr = new Array(5);
arr[0]="1";
arr[1]="8";
arr[2]="28";
arr[3]="18";
arr[4]="88";
function sortNumbers(one,two){
return(one-two);
}
document.write("<b>Original Array is </b>=>"+arr+"<br>");
document.write("<b>Sorted array without calling any function is</b>=>"
  +arr.sort()+"<br> ");
document.write("<b>Sorted array after calling sortNumbers function is
  </b>=>"+arr.sort(sortNumbers)+"<br> ");
</script>
</head>
<body bgcolor="#ddcdfd">
<h2>
Implementing sort() method on numbers
</h2>
</body>
</html>

By running this example on your browser you will get the following output:

Download Source Code

                         

» View all related tutorials
Related Tags: java javascript c string oop array ant function script object diff fun io include help load variable sed print loop

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.