English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to sort a numeric array. I got the code for it. The code is:

It is giving output correctly. But I didn't understand the flow of this code.
Can anyone give explanation for this code. PLS......

2007-07-03 19:44:18 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

I guess you understand the function sortNumber, the declaration of array, populating the array and printing the array on page etc.
The sort() and its interaction with the function sortNumber is actually the question
1. the sort take the elements from arr and pass it to sortNumber
2. sorting can be break down to a lot of comparisons of number pairs, say a is 1st number, b is 2nd number,
3. sortNumber is accept the arguments, and produce what sort() need, if ab positive resulted and there's swap of places, if a=b 0 resulted and no swap of places
4. FYI, if sort() is use without sortNumber, it will then produce the alphabetical(ascii) order of the arr instead.

2007-07-04 11:24:33 · answer #1 · answered by AQuestionMark 7 · 0 0

The function sorNumber takes two arguments and returns the difference between them.

Then a code gets executes as follows:
- An array of 6 elements is declared
- The elements of the array are initialised (one by one)
- The array is displayed in the browser through the document.write() method
- The sorted array is displayed on the browser (you code is missing here).

That's about it.

Hope this helps.

2007-07-03 22:48:30 · answer #2 · answered by Smutty 6 · 0 1

The real magic is internally there is a function Array.sort() and it take in a function pointer; or a delegate as its parameter telling it the direction that it should do it.

2007-07-03 19:52:32 · answer #3 · answered by Andy T 7 · 0 1

indexOf() technique is used to returns the placement of the "first" discovered prevalence of a particular value in a string and lastIndexOf() technique is used to returns the placement of the "final" discovered prevalence of a particular value in a string. Your good judgment is sturdy, thank you!

2016-12-09 00:12:47 · answer #4 · answered by ? 4 · 0 0

fedest.com, questions and answers