Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /* ----------------------------------------------------------------------------
  2. Function: fnc_sortNestedArray2
  3. Based: CBA_fnc_sortNestedArray
  4. Author: Dimon UA
  5. Description:
  6. Used to sort a nested array from lowest to highest or from highest to lowest using quick sort based on the specified column, which must have numerical data.
  7.  
  8. Parameters:
  9. _array: array - Nested array to be sorted
  10. _index: integer - sub array item index to be sorted on
  11. _sort: true - from highest to lowest, false - from lowest to highest
  12.  
  13. Example:
  14. (begin example)
  15. _array = [_array,1,_sort] call fnc_sortNestedArray2
  16. (end)
  17.  
  18. Returns:
  19. Passed in array
  20.  
  21. Author:
  22. Standard algorithm
  23.  
  24. ---------------------------------------------------------------------------- */
  25. /*
  26. Modified BIS function to sort nested arrays.
  27. Added 2nd parameter to indicate which column (index in nested array) to sort on.
  28. Sorts an array of numbers from lowest (left) to highest (right).
  29. The passed array is modified by reference.
  30. This function uses the quick sort algorithm.
  31. */
  32.  
  33. private ["_re_sort","_debug"];
  34. _re_sort = {
  35. private ["_h","_i","_j","_hi","_x","_sort","_id","_lo","_a"];
  36.  
  37. _a = _this select 0; //array to be sorted
  38. _id = _this select 1;
  39. _lo = _this select 2;//lower index to sort from
  40. _hi = _this select 3;//up
  41. _sort = _this select 4;//true - from highest to lowest, false - from lowest to highest
  42.  
  43. _h = nil; //used to make a do-while loop below
  44. _i = _lo;
  45. _j = _hi;
  46. if (count _a == 0) exitWith {};
  47. _x = (_a select ((_lo + _hi) / 2)) select _id;
  48.  
  49. // partition
  50. while {isnil "_h" || {_i <= _j}} do
  51. {
  52. //find first and last elements within bound that are greater / lower than _x
  53. if _sort then
  54. {
  55. while {(_a select _i) select _id > _x} do {_i = (_i) + 1};
  56. while {(_a select _j) select _id < _x} do {_j = (_j) - 1};
  57. }else{
  58. while {(_a select _i) select _id < _x} do {_i = (_i) + 1};
  59. while {(_a select _j) select _id > _x} do {_j = (_j) - 1};
  60. };
  61.  
  62. if (_i <= _j) then {
  63. //swap elements _i and _j
  64. _h = _a select _i;
  65. _a set [_i, _a select _j];
  66. _a set [_j, _h];
  67.  
  68. _i = (_i) + 1;
  69. _j = (_j) - 1;
  70. };
  71. };
  72.  
  73. // recursion
  74. if (_lo < _j) then {[_a, _id, _lo, _j,_sort] call _re_sort};
  75. if (_i < _hi) then {[_a, _id, _i, _hi,_sort] call _re_sort};
  76. };
  77. // and start it off
  78. _debug = false;
  79. [_this select 0, _this select 1, 0, 0 max ((count (_this select 0))-1), _this select 2] call _re_sort;
  80. if _debug then
  81. {
  82. {
  83. call compile format ["
  84. _m%1 = createMarker[""markerBrown%1"",[ _x select 0,_x select 1]];
  85. _m%1 setMarkerShape ""ICON"";
  86. _m%1 setMarkerType ""DOT"";
  87. _m%1 setmarkercolor ""Colorbrown""; ",_forEachIndex];
  88. } foreach (_this select 0);
  89. };
  90. // array is already modified by reference, but return the modified array anyway
  91. _this select 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement