Array.sort() Possibilities : Array « Language Basics « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Language Basics » Array 
Array.sort() Possibilities
  
<HTML>
<HEAD>
<TITLE>Array.sort()</TITLE>
<SCRIPT LANGUAGE="JavaScript1.1">
solarSys = new Array(9)
solarSys[0"M"
solarSys[1"V"
solarSys[2"E"
solarSys[3"M"
solarSys[4"J"
solarSys[5"S"
solarSys[6"U"
solarSys[7"N"
solarSys[8"P"

// comparison functions
function compare1(a,b) {
    // reverse alphabetical order
    if (a > b) {return -1}
    if (b > a) {return 1}
    return 0
}
function compare2(a,b) {
    // last character of planet names
    var aComp = a.charAt(a.length - 1)
    var bComp = b.charAt(b.length - 1)
    if (aComp < bComp) {return -1}
    if (aComp > bComp) {return 1}
    return 0
}
function compare3(a,b) {
    return a.length - b.length
}


function sortIt(form, compFunc) {
    var delimiter = ";"
    if (compFunc == null) {
        solarSys.sort()
    else {
        solarSys.sort(compFunc)
    }
    form.output.value = unescape(solarSys.join(delimiter))
}
</SCRIPT>
<BODY onLoad="document.forms[0].output.value = unescape(solarSys.join(';'))">
<H2>Sorting array elements</H2>
This document contains an array of planets in our solar system.<HR>
<FORM>
Click on a button to sort the array:<P>
<INPUT TYPE="button" VALUE="Alphabetical A-Z" onClick="sortIt(this.form)">
<INPUT TYPE="button" VALUE="Alphabetical Z-A" onClick="sortIt(this.form,compare1)">
<INPUT TYPE="button" VALUE="Last Character" onClick="sortIt(this.form,compare2)">

<INPUT TYPE="button" VALUE="Name Length" onClick="sortIt(this.form,compare3)">
<INPUT TYPE="button" VALUE="Reload Original" onClick="self.location.reload()">
<INPUT TYPE="text" NAME="output" SIZE=62>
</TEXTAREA>
</FORM>
</BODY>
</HTML>

           
         
    
  
Related examples in the same category
1. Demo all methods in Array
2. Assing array value inside function
3. Simple Array Demo
4. Array loop, find:Control array : Two dimension array
5. Reversing, Sorting, and Concatenating an Array
6. Custom Numeric Comparison for the Array.Sort Method
7. Case-Insensitive Comparison for the Array.Sort Method
8. Iterating Through a Sparse Array
9. Using Functions to Iterate Through an Array
10. Reading and Writing Array Elements
11. Array with a numeric parameter and assign data to it
12. A string array
13. Array - properties and methods:length, join, reverse, push,pop,shift
14. Array - sort()
15. Array - concat and slice
16. Array - splice
17. Methods and Properties of the Array Object
18. Displaying the Contents of an Array
19. Using the Array.join() Method
20. Using JavaScript Arrays
21. Extending the Length of an Array
22. An Array within an Array
23. Using the Methods of the Array object
24. Array.reverse() Method
25.  Array Concatenation
26. A Looping Array Lookup
27. A Simple Parallel Array Lookup
28. Adding a prototype Property
29. Two-Dimensional Array Work Around
30. Array definition and iteration
31. Reference an Array by index
32. URL Array
33. Array Utility functions
34. Dynamic array
35. Use for loop to display elements in an array
36. A string array variable
37. Queue based on array
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.