Macro to Sort Slot Contents by Content Type

In case anyone would find this useful.

## macro to sort slot contents by their content type name and return a map of CT names to arrays of assembly items
## Sam Rushing, April 6, 2010
#macro( getSlotContentsByType $result $slotname )
#set($ctnames = {})
#set($result = {})
#set($ctsql = "select t.CONTENTTYPENAME from CONTENTTYPES t where t.CONTENTTYPEID = ")
#initcustomslot($slotname)
#foreach( $assemblyItem in $sys.currentslot.assemblyItems )
#set( $item = $assemblyItem.getNode() )
#set( $thisctid = $item.getProperty('rx:sys_contenttypeid').getLong().toString() )
#if( !$ctnames.get($thisctid) )
#set( $ignored = $ctnames.put($thisctid,$rx.db.get('',$ctsql.concat($thisctid)).get(0).get('CONTENTTYPENAME')))
#end
#set( $thisctname = $ctnames.get($thisctid) )
#if( !$result.keySet().contains($thisctname) )
#set( $ignored = $result.put( $thisctname, [] ) )
#end
#set( $thisresultarray = $result.get($thisctname) )
#set( $ignored = $thisresultarray.add($assemblyItem) )
#end
#endslot($slotname)
#foreach( $ctname in $result.keySet() )
#set( $ignored = $result.put($ctname, $sys.asm.assemble($result.get($ctname))) )
#end
#end## macro: getSlotContentsByType

Dependancy: Custom Slot Macro, PSO Toolkit

Combining this macro with knowledge gained after the original post on how to determine the content type name:


## macro to sort slot contents by their content type name and return a map of CT names to arrays of assembly items
## Sam Rushing, January 23, 2011
#macro( getSlotContentsByType $result $slotname )
#set($result = {})
#initcustomslot($slotname)
#foreach( $assemblyItem in $sys.currentslot.assemblyItems )
#set( $thisctname = $assemblyItem.getNode().getDefinition().getInternalName() )
#if( !$result.keySet().contains($thisctname) )
#set( $ignored = $result.put( $thisctname, [] ) )
#end
#set( $thisresultarray = $result.get($thisctname) )
#set( $ignored = $thisresultarray.add($assemblyItem) )
#end
#endslot($slotname)
#foreach( $ctname in $result.keySet() )
#set( $ignored = $result.put($ctname, $sys.asm.assemble($result.get($ctname))) )
#end
#end## macro: getSlotContentsByType