Posted By

barigazy on 07/17/12


Tagged

sort array


Versions (?)

SORTING MULTIDIM ARRAY BY NAME OR BY COUNT


 / Published in: Maxscript
 

Sort multidimesional array using qsort

  1. fn sortByNameOrCount arr1 arr2 type: maxtomin: =
  2. (
  3. local first, second
  4. case type of (
  5. (#name): (first = arr1[1] ; second = arr2[1])
  6. (#count): (first = arr1[2].count ; second = arr2[2].count)
  7. )
  8. case of (
  9. (first < second): if not maxtomin then -1 else 1
  10. (first > second): if not maxtomin then 1 else -1
  11. default:0
  12. )
  13. )
  14.  
  15. --example_#1 (sort by name alphabetically from Z to A)
  16. multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2)))
  17. qsort multiArr sortByNameOrCount type:#name maxtomin:true
  18. multiArr
  19. --result: #(#("Stalone", #(1, 2, 3, 4, 5)), #("Richard", #(1, 2, 3)), #("Michael", #(1, 2)), #("John", #(1)), #("Branko", #(1, 2, 3, 4)))
  20.  
  21. --example_#2 (sort by name alphabetically from A to Z)
  22. multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2)))
  23. qsort multiArr sortByNameOrCount type:#name maxtomin:false
  24. multiArr
  25. --result: #(#("Branko", #(1, 2, 3, 4)), #("John", #(1)), #("Michael", #(1, 2)), #("Richard", #(1, 2, 3)), #("Stalone", #(1, 2, 3, 4, 5)))
  26.  
  27. --example_#3 (sort by array count from max to min)
  28. multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2)))
  29. qsort multiArr sortByNameOrCount type:#count maxtomin:true
  30. multiArr
  31. --result:#(#("Stalone", #(1, 2, 3, 4, 5)), #("Branko", #(1, 2, 3, 4)), #("Richard", #(1, 2, 3)), #("Michael", #(1, 2)), #("John", #(1)))
  32.  
  33. --example_#4 (sort by array count from min to max)
  34. multiArr = #(#("Branko",#(1,2,3,4)), #("John",#(1)), #("Richard",#(1,2,3)), #("Stalone",#(1,2,3,4,5)), #("Michael",#(1,2)))
  35. qsort multiArr sortByNameOrCount type:#count maxtomin:false
  36. multiArr
  37. --result:#(#("John", #(1)), #("Michael", #(1, 2)), #("Richard", #(1, 2, 3)), #("Branko", #(1, 2, 3, 4)), #("Stalone", #(1, 2, 3, 4, 5)))

Report this snippet  

You need to login to post a comment.