Created
December 2, 2015 15:16
-
-
Save JokerMartini/0ca6a84e08f309a5cdd8 to your computer and use it in GitHub Desktop.
Maxscript: Sorting arrays of arrays and arrays of structs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn sortbyage v1 v2 = | |
( | |
if v1.age < v2.age then -1 else if v1.age > v2.age then 1 else 0 | |
) | |
fn sortbyname v1 v2 = | |
( | |
act = stricmp v1.name v2.name | |
if act == 0 then sortbyage v1 v2 else act | |
) | |
qsort <an_array_of_persons> sortbyname | |
fn sortByNameOrCount arr1 arr2 type: maxtomin: = | |
( | |
local first, second | |
case type of ( | |
(#name): (first = arr1[1] ; second = arr2[1]) | |
(#count): (first = arr1[2].count ; second = arr2[2].count) | |
) | |
case of ( | |
(first < second): if not maxtomin then -1 else 1 | |
(first > second): if not maxtomin then 1 else -1 | |
default:0 | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--example_#1 (sort by name alphabetically from Z to A) | |
multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2))) | |
qsort multiArr sortByNameOrCount type:#name maxtomin:true | |
multiArr | |
--result: #(#("Stalone", #(1, 2, 3, 4, 5)), #("Richard", #(1, 2, 3)), #("Michael", #(1, 2)), #("John", #(1)), #("Branko", #(1, 2, 3, 4))) | |
--example_#2 (sort by name alphabetically from A to Z) | |
multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2))) | |
qsort multiArr sortByNameOrCount type:#name maxtomin:false | |
multiArr | |
--result: #(#("Branko", #(1, 2, 3, 4)), #("John", #(1)), #("Michael", #(1, 2)), #("Richard", #(1, 2, 3)), #("Stalone", #(1, 2, 3, 4, 5))) | |
--example_#3 (sort by array count from max to min) | |
multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2))) | |
qsort multiArr sortByNameOrCount type:#count maxtomin:true | |
multiArr | |
--result:#(#("Stalone", #(1, 2, 3, 4, 5)), #("Branko", #(1, 2, 3, 4)), #("Richard", #(1, 2, 3)), #("Michael", #(1, 2)), #("John", #(1))) | |
--example_#4 (sort by array count from min to max) | |
multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2))) | |
qsort multiArr sortByNameOrCount type:#count maxtomin:false | |
multiArr | |
--result:#(#("John", #(1)), #("Michael", #(1, 2)), #("Richard", #(1, 2, 3)), #("Branko", #(1, 2, 3, 4)), #("Stalone", #(1, 2, 3, 4, 5))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment