forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortArray.ahk
54 lines (53 loc) · 1.9 KB
/
SortArray.ahk
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
sortArray(byRef a, Ascending := True)
{
for index, value in a
out .= value "-" index "|" ; "-" allows for sort to work with just the value
; out will look like: value-index|value-index|
v := a[a.minIndex()]
if v is number
type := " N "
StringTrimRight, out, out, 1 ; remove trailing |
Sort, out, % "D| " type (!Ascending ? " R" : " ")
aStorage := []
loop, parse, out, |
{
StringSplit, split, A_LoopField, -
; split1 = value, split2 = index
aStorage.insert(a[split2])
}
a := aStorage
return
}
/*
SortArray(Array, Order="A") {
;Order A: Ascending, D: Descending, R: Reverse
MaxIndex := ObjMaxIndex(Array)
If (Order = "R") {
count := 0
Loop, % MaxIndex
ObjInsert(Array, ObjRemove(Array, MaxIndex - count++))
Return
}
Partitions := "|" ObjMinIndex(Array) "," MaxIndex
Loop {
comma := InStr(this_partition := SubStr(Partitions, InStr(Partitions, "|", False, 0)+1), ",")
spos := pivot := SubStr(this_partition, 1, comma-1) , epos := SubStr(this_partition, comma+1)
if (Order = "A") {
Loop, % epos - spos {
if (Array[pivot] > Array[A_Index+spos])
ObjInsert(Array, pivot++, ObjRemove(Array, A_Index+spos))
}
} else {
Loop, % epos - spos {
if (Array[pivot] < Array[A_Index+spos])
ObjInsert(Array, pivot++, ObjRemove(Array, A_Index+spos))
}
}
Partitions := SubStr(Partitions, 1, InStr(Partitions, "|", False, 0)-1)
if (pivot - spos) > 1 ;if more than one elements
Partitions .= "|" spos "," pivot-1 ;the left partition
if (epos - pivot) > 1 ;if more than one elements
Partitions .= "|" pivot+1 "," epos ;the right partition
} Until !Partitions
}
*/