-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_GroupSort.ahk
122 lines (96 loc) · 3.54 KB
/
class_GroupSort.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
###############################################################################
GroupSort v0.1 by Avi Aryan
Sorts multiple related lists together taking any of those lists as a base
###############################################################################
SEE EXAMPLES AT -
https://github.com/avi-aryan/Avis-Autohotkey-Repo/blob/master/Example_files/GroupSort-examples.ahk
MY SCRIPTS -
http://avi-aryan.github.io/Autohotkey.html
*/
class GroupSort
{
;------------------------------------------
;New GroupSort()
; object - The lists stored in a single object such that the host (parent) object occupies Index 1 and all others 2,3,4.....
; options - The options used for Sorting. For details, see the Sort command in Autohotkey help
__New(object, options="")
{
if !Isobject(object)
return -1
m_item := object[1]
m_delim := Substr(options, ( delim_isp := Instr(options, "D") )+1, 1) , m_delim := delim_isp ? m_delim : "`n"
str_obj := {}
loop % object.MaxIndex()-1 ;Adding alternative data to objects
{
temp := object[A_index+1] , tempobj := {}
loop, parse, temp,% m_delim
tempobj[A_index] := A_LoopField
str_obj[A_index] := tempobj
}
length := 0 , len_obj := {} ;Get max m_item length
loop, parse, m_item,% m_delim
length := ( temp := StrLen(A_LoopField) ) > length ? temp : length , len_obj[A_index] := temp
loop, parse, m_item,% m_delim ;Add tabs
m_item2 .= A_LoopField this.Genitem("`t", length-len_obj[A_index]) A_space A_index m_delim ;A_space is a security factor
m_item2 := Substr(m_item2, 1, -Strlen(m_delim))
Sort, m_item2,% options ;Sort
str_obj_ind := str_obj.MaxIndex() ;RE-ARRANGING ALL DATA
loop, parse, m_item2,% m_delim
{
temp_ind := Substr(A_LoopField, Instr(A_LoopField, " ", 0, 0)+1)
temp_length := length - len_obj[temp_ind]
m_item3 .= Substr(A_LoopField, 1, -temp_length-1-Strlen(temp_ind)) m_delim
loop % str_obj_ind
temp_obj := str_obj[A_index] , str_item%A_index% .= temp_obj[temp_ind] m_delim ;str_item1,2,3... contains changed data
}
m_item3 := Rtrim(m_item3, m_delim) ;Creating return object
this.retobj := {}
this.retobj[1] := m_item3
loop % str_obj_ind
this.retobj[A_index+1] := Rtrim(str_item%A_index%, m_delim)
this.m_delim := m_delim ;Used below
}
;-------------------------------------------------------
;Sort()
; returns Sorted data in object format
; If columns=0, all the columns (LISTS) are returned in Object format with the parent list occupying Index 1 of the Object
; If columns is a CSV, then the matched coulums are returned such that the parent list is Column 1
Sort(Columns=0) ;Add many columns in CSV format
{
if !Columns
return this.retobj
else
{
retobj2 := {}
loop, parse, Columns, `,,%A_space%%A_tab%
this.retobj2[A_index] := this.retobj[A_LoopField]
return this.retobj2
}
}
;-------------------------------------------------------
;Fetch()
; returns data in string format
;
; Columns = <same as in .Sort()>
; whitespaces = separation between different columns
; wh_item = the item that is repeated <whitespaces> times to give the separation
Fetch(Columns=0, whitespaces=2, wh_item="`n")
{
delim := this.Genitem(wh_item, whitespaces)
if !Columns
loop % this.retobj.MaxIndex()
toret .= this.retobj[A_Index] delim
else
loop, parse, Columns, `,
toret .= this.retobj[A_LoopField] delim
return Substr(toret, 1, -Strlen(delim))
}
;---------------------------------------------------------
;HelpeR Functions
Genitem(item, N){
loop % N
t .= item
return t
}
}