workflow::tree::sorter::sort_list_of_lists (public)

 workflow::tree::sorter::sort_list_of_lists -list list -sort_by sort_by \
    -object_id object_id -parent_object_id parent_object_id

Defined in packages/workflow/tcl/workflow-tree-procs.tcl

Sort the given list-of-lists. The list should have 3-4 columns:

  • Display Name (should have any padding you want)
  • Object ID
  • Parent Object ID
  • Object Name

The display name is the only column which is not critical for sorting the tree.

Example:


    # Get a list that can be used as the options of a 'select' widget in ad_form
    set directories [db_list_of_lists get_directories {
	select  lpad(' ', (level-1)*4*6 + 1, ' ') || directory_name as directory_name,
	                 directory_id,
	                 parent_directory_id,
	                 directory_name
	           from  directories
	         start   with parent_directory_id    is null
	         connect by prior directory_id       = parent_directory_id
    }]
     
    # Sort the options
    set directories [tree::sorter::sort_lists_of_lists -list $directories -sort_by 3 -object_id 1 -parent_object_id 2]
    

Switches:
-list
(required)
The list-of-lists that should be sorted as a tree.
-sort_by
(required)
The index of the element in each row which should be used to sort by.
-object_id
(required)
The index of the element in each row which uniquely identifies it.
-parent_object_id
(required)
The index of the element in each row which contains the unique identifier of its parent.
Author:
helsleya@cs.ucr.edu

Partial Call Graph (max 5 caller/called nodes):
%3

Testcases:
No testcase defined.
Source code:
    # Setup the format for sort-keys
    set max_length_of_sort_by 0
    foreach row $list {
    set length_of_sort_by [string length [lindex $row $sort_by]]
    if {$length_of_sort_by > $max_length_of_sort_by} {
        set max_length_of_sort_by $length_of_sort_by
    }
    }
    set sort_key_format "%-${max_length_of_sort_by}s%08s"
    
    # Setup the list to save results in and two stacks
    set tree                        [list]
    set sort_key_stack              [list]
    set object_id_stack             [list]
    
    # Make a full-sort-key for each row
    foreach row $list {
    set oid                 [lindex $row $object_id]
    set parent_id           [lindex $row $parent_object_id]
    set sort_key            [format $sort_key_format [lindex $row $sort_by$parent_id]
          
    # Find parent in stack
    set pos_in_stack        [expr {[lsearch $object_id_stack $parent_id] + 1}]
    
    # Clear anything after parent from the stack
    if {$pos_in_stack < [llength $object_id_stack]} {
        set object_id_stack     [lreplace $object_id_stack       $pos_in_stack          end                           ]
        set sort_key_stack      [lreplace $sort_key_stack        $pos_in_stack          end                           ]
    }
          
    # Push the current object on the stack
    lappend object_id_stack     $oid
    lappend sort_key_stack      $sort_key
    
    # Save the full_sort_key with the row in the tree
    set full_sort_key           [join $sort_key_stack //]
    lappend row                 $full_sort_key
    lappend tree                $row
    }
    
    # Sort the rows
    set tree [lsort -dictionary -index end $tree]
    
    return $tree
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: