-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path549.html
More file actions
55 lines (53 loc) · 1.77 KB
/
Copy path549.html
File metadata and controls
55 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<h1 id="sort">SORT</h1>
<blockquote>
<p>SORT byref A [USE cmpfunc( x, y )]</p>
</blockquote>
<p>Sorts an array <code>A</code> in ascending order. The sorted array is
return as <code>A</code>, therefore overwriting the initial array. If a
compare function <code>cmpfunc</code> is specified, this function will
be used for comparision. The compare function takes two elements of
<code>A</code> as <code>x</code>, <code>y</code> to compare and must
return:</p>
<ul>
<li><code>-1</code> if <code>x</code> is to be placed before
<code>y</code></li>
<li><code>1</code> if <code>y</code> is to be placed before
<code>x</code></li>
<li><code>0</code> if it doesn’t matter which is placed first (which is
usually the case when the elements are equal)</li>
</ul>
<h3 id="example-1-sorting-in-ascending-order">Example 1: Sorting in
ascending order</h3>
<pre><code>A = [5, 3, 8, 2, 1, 7, 9]
sort A
print A ' Output [1,2,3,5,7,8,9]</code></pre>
<h3
id="example-2-sorting-in-ascending-order-using-a-compare-function">Example
2: Sorting in ascending order using a compare function</h3>
<pre><code>func cmpfunc_ascending(x, y)
if x == y
return 0
elseif x > y
return 1
else
return -1
endif
end
A = [5, 3, 8, 2, 1, 7, 9]
sort A use cmpfunc_ascending(x, y)
print A ' Output [1,2,3,5,7,8,9]</code></pre>
<h3
id="example-3-sorting-in-descending-order-using-a-compare-function">Example
3: Sorting in descending order using a compare function</h3>
<pre><code>func cmpfunc_descending(x, y)
if x == y
return 0
elseif x < y
return 1
else
return -1
endif
end
A = [5, 3, 8, 2, 1, 7, 9]
sort A use cmpfunc_descending(x, y)
print A ' Output [9,8,7,5,3,2,1]</code></pre>