按名称或计数排序多维数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按名称或计数排序多维数组相关的知识,希望对你有一定的参考价值。

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)))

以上是关于按名称或计数排序多维数组的主要内容,如果未能解决你的问题,请参考以下文章

如何排序多维数组的第二个索引?

如何在 PHP 中循环遍历多维数组并按名称递归删除键?

按多列对多维数组进行排序

按子项的值对多维数组进行排序

C#按多列对多维数组进行排序

Java中的多维数组按某列排序的方法