mozilla

Compare Revisions

Array.prototype.sort()

Change Revisions

Revision 429841:

Revision 429841 by dbruant on

Revision 429843:

Revision 429843 by dbruant on

Title:
Array.prototype.sort
Array.prototype.sort
Slug:
Web/JavaScript/Reference/Global_Objects/Array/sort
Web/JavaScript/Reference/Global_Objects/Array/sort
Tags:
"Array", "Array methods"
"Array", "Array methods"
Content:

Revision 429841
Revision 429843
n7    <h2 id="Summary" name="Summary">n
8      Summary
9    </h2>
10    <p>
11      Sorts the elements of an array in place and returns the arr
>ay. 
12    </p>7    <p>
13    <table class="standard-table">8      Sorts the elements of an array in place and returns the arr
 >ay. The sort is not necessarily <a href="https://en.wikipedia.org
 >/wiki/Sorting_algorithm#Stability" title="https://en.wikipedia.or
 >g/wiki/Sorting_algorithm#Stability">stable</a>.
14      <thead>9    </p>
15        <tr>
16          <th class="header" colspan="2">
17            Method of <a href="/en-US/docs/JavaScript/Reference/G
>lobal_Objects/Array" title="JavaScript/Reference/Global_Objects/A 
>rray"><code>Array</code></a> 
18          </th>
19        </tr>
20      </thead>
21      <tbody>
22        <tr>
23          <td>
24            Implemented in
25          </td>
26          <td>
27            JavaScript 1.1
28          </td>
29        </tr>
30        <tr>
31          <td>
32            ECMAScript Edition
33          </td>
34          <td>
35            ECMAScript 1st Edition
36          </td>
37        </tr>
38      </tbody>
39    </table>
n108      The sort()&nbsp;method can be conveniently used with <a hren78      The <code>sort</code> method can be conveniently used with 
>f="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Refe
>Operators/function" title="https://developer.mozilla.org/en-US/do>rence/Operators/function" title="https://developer.mozilla.org/en
>cs/JavaScript/Reference/Operators/function">function expressions<>-US/docs/JavaScript/Reference/Operators/function">function expres
>/a>&nbsp;(and <a href="https://developer.mozilla.org/en-US/docs/J>sions</a>&nbsp;(and <a href="https://developer.mozilla.org/en-US/
>avaScript/Guide/Closures" title="https://developer.mozilla.org/en>docs/JavaScript/Guide/Closures" title="https://developer.mozilla.
>-US/docs/JavaScript/Guide/Closures">closures</a>):>org/en-US/docs/JavaScript/Guide/Closures">closures</a>):
n163print("stringArray: " + stringArray.join() +"\n");n133console.log("stringArray:", stringArray.join());
164print("Sorted: " + stringArray.sort() +"\n\n");134console.log("Sorted:", stringArray.sort());
n166print("numberArray: " + numberArray.join() +"\n");n136console.log("numberArray:", numberArray.join());
167print("Sorted without a compare function: " + numberArray.sort() 137console.log("Sorted without a compare function:", numberArray.sor
>+"\n");>t());
168print("Sorted with compareNumbers: " + numberArray.sort(compareNu138console.log("Sorted with compareNumbers:", numberArray.sort(compa
>mbers) +"\n\n");>reNumbers));
n170print("numericStringArray: " + numericStringArray.join() +"\n");n140console.log("numericStringArray:", numericStringArray.join());
171print("Sorted without a compare function: " + numericStringArray.141console.log("Sorted without a compare function:", numericStringAr
>sort() +"\n");>ray.sort());
172print("Sorted with compareNumbers: " + numericStringArray.sort(co142console.log("Sorted with compareNumbers:", numericStringArray.sor
>mpareNumbers) +"\n\n");>t(compareNumbers));
n174print("mixedNumericArray: " + mixedNumericArray.join() +"\n");n144console.log("mixedNumericArray:", mixedNumericArray.join());
175print("Sorted without a compare function: " + mixedNumericArray.s145console.log("Sorted without a compare function:", mixedNumericArr
>ort() +"\n");>ay.sort());
176print("Sorted with compareNumbers: " + mixedNumericArray.sort(com146console.log("Sorted with compareNumbers:", mixedNumericArray.sort
>pareNumbers) +"\n\n");>(compareNumbers));
nn176 
n207var map = [];n178var map = list.map(function(e, i){
208// container for the resulting order179  return {index: i, value: e.toLowerCase()}
209var result = [];180})
210 
211// walk original array to map values and positions
212for (var i=0, length = list.length; i &lt; length; i++) {
213  map.push({    
214    // remember the index within the original array
215    index: i, 
216    // evaluate the value to sort
217    value: list[i].toLowerCase()
218  });
219}
n226// copy values in right ordern187// container for the resulting order
227for (var i=0, length = map.length; i &lt; length; i++) {188var result = map.map(function(e){
228  result.push(list[map[i].index]);189  return list[e.index]
229}190})
230 
231// print sorted list
232print(result);
n285    <h2>n243    <h2 id="Browser_compatibility">
t387    <h2 name="See_also">t345    <h2 id="See_also" name="See_also">

Back to History