A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Introduction to arrays, sorting arraysAn introduction to JavaScript arrays, how to create and access arrays. Also shows you how to use the sort() method to sort both string and number arrays. |
On Saturday, July 30th 2005 at 12:55 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 3.5 with 6 votes) |
||||||||||||
|
Contextual Ads
More JavaScript Resources
Advertisement
Let's see how we can create an array containing Microsoft's latest operating systems:
An array is declared similar to a variable, using the var keyword, but also assign it to new Array();. This lets JavaScript know OperatingSystems is not just a variable, it's an array. Array's elements are accessed using their index, which starts from 0. That's why we have 7 operating systems in the array (and 7 elements), but since they're counted from 0, we end up with the 7th element having index 6. Now let's see how we can access the elements:
The following line along with the declaration of the array will result in the following string: The Windows XP operating system is the predecessor of the new Windows Vista operating system. We can do many other things using an array, like loop through it:
Looping through arrays is a common thing, and one of the main advantages of arrays. We initialize a variable, i, and set it to 0. We increment it with each loop until it reaches the length of the OperatingSystems array. We can get the length of an array using the length property. In our case the length property returns 7 (the number of elements the array has). JavaScript arrays don't have a fixed size, you can add as many elements as you want inside an array. However, you sometimes may want to define a size for the array, so that you can limit the number of elements the array can store. You can do this when defining the array, in the example below we limit the OperatingSystems array to 10, this way you won't be able to add more than 10 operating systems into the array:
Also, there is a different way you can use to define an array. In the example below the elements are added in the array right when the array is initialized:
This way of adding elements to an array has the same result as when adding them separately (like we did in the first example), however when you have really long arrays, it can get annoying. Sorting arraysUsing the sort() method we can easily sort the elements inside an array. sort() can sort both arrays of numbers and strings. Let's try this on a new array I defined below.
We have the characters from the popular show Seinfeld in an array, ordered randomly, and we want to sort them by name. The following line should do it:
Now the array is sorted alphabetically, ascending (which is the default). Let's loop through it like we did with the OperatingSystems array and see the order:
The result is exactly what we wanted: At index 0 we have Cosmo Kramer At index 1 we have Elaine Benes At index 2 we have George Costanza At index 3 we have Jerry Seinfeld Now let's sort some numbers. We create a new array:
This array contains some randomly ordered prime numbers. We want to put them in ascending order, so we use the sort() method and then we loop through the array:
And the results is... At index 0 we have 11 At index 1 we have 13 At index 2 we have 17 At index 3 we have 19 At index 4 we have 2 At index 5 we have 23 At index 6 we have 29 At index 7 we have 3 At index 8 we have 5 At index 9 we have 7 Oh boy, what went wrong? Well, sort() sorts by the ASCII value of the character, that's why we first get the numbers that start with 1 (11, 13, 17, 19), then with 2 (2, 23, 29) and then the rest (3, 5, 7). What we need to be able to sort these numbers correctly is a comparison function. Let's see how it looks like:
It returns the difference between two numbers. How does this help? Well if the returned value is negative, it means X is smaller than Y. If the returned number is positive, it means X is bigger than Y. If the returned value is 0, it means both are equal. Now how we use this function with sort()? Simple:
We now have the result we want: At index 0 we have 2 At index 1 we have 3 At index 2 we have 5 At index 3 we have 7 At index 4 we have 11 At index 5 we have 13 At index 6 we have 17 At index 7 we have 19 At index 8 we have 23 At index 9 we have 29 There are many other things we can do with JavaScript's arrays, that's why this will be continued. |
|||||||||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||||||||||||
|
|||||||||||||
Current Comments
Related Tutorials
Related Source Code
JavaScript Job Search