源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
5.1 KiB

  1. SortTools = {}
  2. --[[
  3. ()
  4. @para2 sort_key_name key
  5. --]]
  6. function SortTools.KeyLowerSorter(sort_key_name)
  7. return function(a, b)
  8. if a[sort_key_name] < b[sort_key_name] then
  9. return true
  10. else
  11. return false
  12. end
  13. end
  14. end
  15. --[[
  16. ()
  17. @para2 sort_key_name key
  18. --]]
  19. function SortTools.KeyUpperSorter(sort_key_name)
  20. return function(a, b)
  21. if a[sort_key_name] > b[sort_key_name] then
  22. return true
  23. else
  24. return false
  25. end
  26. end
  27. end
  28. --[[
  29. @param tbl table
  30. @param arg key
  31. key对应的排序方式:Array.UPPER还是降序:Array.LOWER
  32. @arg = {{key1,key2,...},{Array.UPPER,Array.LOWER,...}}
  33. Array.UPPER
  34. @author jkz
  35. @create 12/18/2013
  36. --]]
  37. function SortTools.MoreKeysSorter(tbl, ...)
  38. local arg = {...}
  39. local func = nil
  40. --参数为空的时候直接对表项排序--
  41. if arg == nil or #arg == 0 then
  42. func = SortTools.ItemUpperSorter()
  43. end
  44. local argName = arg[1]
  45. local argType = arg[2]
  46. local sort_func = nil
  47. if argName == nil then
  48. func = SortTools.ItemUpperSorter()
  49. --参数为单个的时候调用单个排序函数--
  50. elseif type(argName) ~= "table" or #argName <= 1 then
  51. if argType == nil or argType == Array.UPPER then
  52. func = SortTools.KeyUpperSorter(argName)
  53. else
  54. func = SortTools.KeyLowerSorter(argName)
  55. end
  56. end
  57. --多参数排序方法--
  58. func = function(a, b)
  59. local len = #argName
  60. local i = 1
  61. while len > 0 do
  62. if argType == nil or argType[i] == nil or argType[i] == Array.UPPER then
  63. if a[argName[i]] > b[argName[i]] then
  64. return true
  65. elseif a[argName[i]] ~= b[argName[i]] then
  66. return false
  67. end
  68. else
  69. if a[argName[i]] < b[argName[i]] then
  70. return true
  71. elseif a[argName[i]] ~= b[argName[i]] then
  72. return false
  73. end
  74. end
  75. len = len - 1
  76. i = i + 1
  77. end
  78. return false
  79. end
  80. table.sort(tbl, func)
  81. end
  82. --[[
  83. ()
  84. --]]
  85. function SortTools.ItemLowerSorter()
  86. return function(a, b)
  87. return a < b
  88. end
  89. end
  90. --[[
  91. ()
  92. --]]
  93. function SortTools.ItemUpperSorter()
  94. return function(a, b)
  95. return a > b
  96. end
  97. end
  98. function SortTools.BinaryInsert(t, value, fcomp)
  99. -- Initialise compare function
  100. fcomp = fcomp or SortTools.ItemLowerSorter
  101. -- Initialise numbers
  102. local istart, iend, imid, istate = 1, #t, 1, 0
  103. -- Get insert position
  104. while istart <= iend do
  105. -- calculate middle
  106. imid = math.floor( (istart+iend)/2 )
  107. -- compare
  108. if fcomp( value, t[imid] ) then
  109. iend, istate = imid - 1, 0
  110. else
  111. istart, istate = imid + 1, 1
  112. end
  113. end
  114. table.insert(t, (imid + istate), value)
  115. return (imid + istate)
  116. end
  117. function SortTools.BinarySearch(t, value, equal_func, compare_func)
  118. -- Initialise numbers
  119. local max_t = #t
  120. local istart, iend, imid = 1, max_t, 0
  121. --[[
  122. if istart == iend then
  123. local find_t = nil
  124. if equal_func(t[istart], value) then
  125. find_t = {istart, iend}
  126. end
  127. return find_t
  128. end
  129. ]]
  130. -- Binary Search
  131. while istart <= iend do
  132. -- calculate middle
  133. imid = math.floor( (istart+iend)/2 )
  134. -- get compare value
  135. local value2 = t[imid]
  136. -- get all values that match
  137. if equal_func(value, value2) then
  138. local tfound,num = { imid,imid },imid - 1
  139. while (num >= 1) and (equal_func(value, t[num])) do
  140. tfound[1],num = num,num - 1
  141. end
  142. num = imid + 1
  143. while (num <= max_t) and (equal_func(value, t[num])) do
  144. tfound[2],num = num,num + 1
  145. end
  146. return tfound
  147. -- keep searching
  148. elseif compare_func(value, value2) then
  149. iend = imid - 1
  150. else
  151. istart = imid + 1
  152. end
  153. end
  154. end
  155. --冒泡排序
  156. --author:sunny
  157. --list:待排序列表
  158. --sort_word:排序元素
  159. --descending:默认 降序,false 升序
  160. function SortTools.BubbleSort(list,sort_word,descending)
  161. local comp = function(item0,item1)
  162. --将空项排在后面
  163. if item0[sort_word] == nil and item0[sort_word] ~= nil then
  164. return true
  165. end
  166. if item0[sort_word] ~= nil and item1[sort_word] ~= nil then
  167. if descending == false then
  168. --升序
  169. if item0[sort_word] > item1[sort_word] then
  170. return true
  171. end
  172. else
  173. --降序
  174. if item0[sort_word] < item1[sort_word] then
  175. return true
  176. end
  177. end
  178. end
  179. return false
  180. end
  181. local length = #(list)
  182. local result = false
  183. local temp = nil
  184. local flag = true
  185. for i=1,length - 1 do
  186. if not flag then
  187. break
  188. end
  189. flag = false
  190. for k=1,length - i do
  191. result = comp(list[k],list[k+1])
  192. if result then
  193. flag = true
  194. temp = list[k]
  195. list[k] = list[k+1]
  196. list[k+1] = temp
  197. end
  198. end
  199. end
  200. end
  201. --根据func进行冒泡
  202. --返回true,则将后一个替换前一个
  203. --返回false,则不作处理
  204. function SortTools.SortListByFunc(list, func)
  205. local length = #(list)
  206. local result = false
  207. local temp = nil
  208. local flag = true
  209. for i=1,length - 1 do
  210. if not flag then
  211. break
  212. end
  213. flag = false
  214. for k=1,length - i do
  215. result = func(list[k],list[k+1])
  216. if result then
  217. flag = true
  218. temp = list[k]
  219. list[k] = list[k+1]
  220. list[k+1] = temp
  221. end
  222. end
  223. end
  224. end