-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortFunctions.lua
208 lines (180 loc) · 5.68 KB
/
SortFunctions.lua
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
--
-- Merchant Plus
-- A Modern Scrollable UI for Merchants
--
-- Copyright 2023 - 2024 SimGuy
--
-- Use of this source code is governed by an MIT-style
-- license that can be found in the LICENSE file or at
-- https://opensource.org/licenses/MIT.
--
local _, Shared = ...
-- Push us into shared object
local Sort = {}
Shared.Sort = Sort
-- This is a list of supported cell types to be filled later
Sort.CellTypes = {}
-- This is a list of supported columns to be filled later
Sort.Columns = {}
local CurrencyCache = {}
-- Item Price: sort by magic:
-- Items with regular gold prices first, in cost order
-- Items with extended cost currency, ordered most to least significant
-- First by name of currency, then by amount of currency
function Sort:Price(lhs, rhs)
local result = nil
-- If extendedCost state is the same, compare the values
if lhs['extendedCost'] == rhs['extendedCost'] then
-- This is just gold, check which side is higher
if not lhs['extendedCost'] then
-- If the value is the same, allow to fall back to index
if lhs['price'] ~= rhs['price'] then
result = lhs['price'] < rhs['price']
end
-- This is extended cost, compare the currencies
else
-- Get the number of currencies required for each item
local lhitems = GetMerchantItemCostInfo(lhs['index'])
local rhitems = GetMerchantItemCostInfo(rhs['index'])
local difference = 0
-- Loop through all currencies
for i = 1, MAX_ITEM_COST do
-- If we've already found a difference, stop
if difference ~= 0 then
break
end
-- Get the currency item and number required
local li = 1 + lhitems - i
local ri = 1 + rhitems - i
local _, lhvalue, lhlink, lhname = GetMerchantItemCostItem(lhs['index'], li)
local _, rhvalue, rhlink, rhname = GetMerchantItemCostItem(rhs['index'], ri)
-- If one side has something and the other has nothing
-- put those with no item first
if lhvalue <= 0 and rhvalue > 0 then
difference = -1
elseif lhvalue > 0 and rhvalue <= 0 then
difference = 1
elseif lhvalue > 0 and rhvalue > 0 then
-- If this is an item instead of a currency, get its name
if not lhname then
lhname = Sort:GetCurrencyName(lhlink)
end
if not rhname then
rhname = Sort:GetCurrencyName(rhlink)
end
-- Sort the currency by name first
-- If names are the same, sort by value
-- If values are the same, fall back to index
local namecheck = SortUtil.CompareUtf8i(lhname, rhname)
if namecheck == 0 then
if lhvalue < rhvalue then
difference = -1
elseif lhvalue > rhvalue then
difference = 1
end
else
difference = namecheck
end
end
end
-- If the sort indicated there was a difference, return that result,
-- otherwise we fall back to the sort by index.
if difference ~= 0 then
result = difference == -1
end
end
else
if lhs.extendedCost then
result = false
else
result = true
end
end
return result
end
-- Cache currency names to reduce calls to GetItemInfo()
function Sort:GetCurrencyName(link)
local id = C_Item.GetItemInfoInstant(link)
local name
if CurrencyCache[id] then
name = CurrencyCache[id]
else
name = C_Item.GetItemInfo(link)
if name then
CurrencyCache[id] = name
else
name = ""
end
end
return name
end
-- This function will sort based on the request
function Sort:Sort(left, right)
local order, state = self:GetSortOrder()
local lhs = left
local rhs = right
-- Flip left and right if the sort is reversed
if state == 1 then
lhs = right
rhs = left
end
-- The sort method will sometimes send nil values
if not lhs or not rhs then
return false
end
-- Default sort by Merchant index if nothing else is set.
-- Use this to ensure that equal values retain a deterministic
-- sort, otherwise race conditions may occur where they shift
-- randomly.
local result = lhs['index'] < rhs['index']
local col = Sort.Columns[order]
if col then
if col.sortfunction then
-- Handle custom sort function if provided
local sort = col.sortfunction(nil, lhs, rhs)
if sort ~= nil then
result = sort
end
elseif col.field then
local key = col.field
-- Handle item sort (by name only)
if col.celltype == Sort.CellTypes.Item then
local namecheck = SortUtil.CompareUtf8i(lhs[key] or "", rhs[key] or "")
if namecheck ~= 0 then
result = namecheck == -1
end
-- Handle number sort
elseif col.celltype == Sort.CellTypes.Number then
if lhs[key] ~= rhs[key] and lhs[key] ~= nil and rhs[key] ~= nil then
result = lhs[key] < rhs[key]
end
-- Handle text sort
elseif col.celltype == Sort.CellTypes.Text then
local namecheck = SortUtil.CompareUtf8i(lhs[key] or "", rhs[key] or "")
if namecheck ~= 0 then
result = namecheck == -1
end
-- Handle icon sort
-- This tries to sort by number, or else UTF8 comparison if these aren't numbers
-- If something weird is stashed in here, a custom sort function should be used
elseif col.celltype == Sort.CellTypes.Icon then
if type(lhs[key]) == "number" and type(rhs[key]) == "number" then
if lhs[key] ~= rhs[key] then
result = lhs[key] < rhs[key]
end
else
local namecheck = SortUtil.CompareUtf8i(lhs[key] or "", rhs[key] or "")
if namecheck ~= 0 then
result = namecheck == -1
end
end
-- Handle boolean sort
elseif col.celltype == Sort.CellTypes.Boolean then
if lhs[key] ~= rhs[key] then
result = lhs[key]
end
end
end
end
return result
end