forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListSort.calc
55 lines (42 loc) · 1.29 KB
/
ListSort.calc
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
Case (
//-- One value left in list - return just that value
ValueCount ( theList ) <= 1 ;
GetValue ( theList ; 1 ) ;
// Default
Let ( [
// split list in two
length = ValueCount ( theList ) ;
leftLength = Floor ( length / 2 ) ;
rightLength = length - leftLength ;
leftList = LeftValues ( theList ; leftLength ) ;
rightList = RightValues ( theList ; rightLength ) ;
// sort each sub-list
sortedLeft = ListSort ( leftList ) ;
sortedRight = ListSort ( rightList ) ;
// merge the two sorted lists
mergedList = ListSortLoop ( sortedLeft ; sortedRight )
] ;
TrimCR ( mergedList )
)
)
/* __________________________________________________
NAME:
ListSort ( theList )
v1.1
PURPOSE:
Sort a return-delimited list using the “mergesort” algorithm
HISTORY:
Created by Soliant Consulting (See below or FM 8 Functions p. 228)
Modified 2008.06.17 by DChandler for formatting/commenting preference
Modified 2010.11.10 by DChandler to trim trailing carriage return
INPUT:
list: return-delimited list of values
OUTPUT:
The same list, sorted ascending
DEPENDENCIES:
Custom Functions: ListSortLoop(), TrimCR()
Function created by Soliant Consulting
www.soliantconsulting.com
Released under the Creative Commons Attribution 2.5 License
http://creativecommons.org/licenses/by/2.5/
*/