please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Array.sort() Method

Name

Array.sort() Method---sort the elements of an array

Availability

Navigator 3.0

Synopsis

array.sort()
array.sort(orderfunc)

Arguments

orderfunc

An optional function used to specify the sorting order.

Returns

Nothing.

Description

The sort() method sorts the elements of array in place--i.e., no copy of the array is made. If sort() is called with no arguments, then the elements of the array are arranged in alphabetical order (more precisely: the order determined by the character encoding). To do this, elements are first converted to strings, if necessary, so that they can be compared.

If you want to sort the array elements in some other order, you must supply a comparison function that compares two values and returns a number indicating their relative order. The comparison function should take two arguments, a and b, and should:

The example section shows how you might write a comparison function to sort an array of numbers in numerical, rather than alphabetical order.

Example

The following code shows how you can write an ordering function and use it to sort an array:

// An ordering function for a numerical sort
function numberorder(a, b) { return a - b; }
a =index.html new Array(33, 4, 1111, 222);
a.sort();             // Alphabetical sort:  1111, 222, 33, 4
a.sort(numberorder);  // Numerical sort: 4, 33, 222, 1111

See Also

"Array", "Array.reverse()"


Previous Home Next
Array.reverse() Book Index assign()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/refp_13.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/11/15 02:26,
TOP NEW HELP FIND: 
3.148.115.16:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/language/java/script/definitive/refp_13.htm"> [Chapter 21] Reference: Array.sort()</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?