How can I reorder active menu items programmatically? I have an 'audience' menu which has 4 links. When a user visits one of these links, it becomes active, and I want to push it to the front of the menu item list. Outcomes would like like;

Pitchers | Catchers | Hitters | Coaches
Coaches (active) | Pitchers | Catchers | Hitters
Catchers (active) | Pitchers | Hitters | Coaches
Hitters (active) | Pitchers | Catchers | Coaches


How can I accomplish this programmatically? I can't seem to find the right theme_ or hook_ api function to pull this off.

I've tried this, and changing the '#original_link' weight didn't seem to make a difference (note, I have other things going on here):

function apple_menu_link__menu_audience_gateway(&amp;$vars) {
  $element = $vars['element'];
  $submenu = '';

  if($element['#original_link']['in_active_trail'] == TRUE) {
    $element['#original_link']['weight'] = 50;
  }

  if ($element['#below']) {
    unset($element['#below']['#theme_wrappers']);
    $submenu = '&lt;ul class="nav"&gt;' . drupal_render($element['#below']) . '&lt;/ul&gt;';
  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);

  return '&lt;li' . drupal_attributes($element['#attributes']) . '&gt;' . $output . $submenu . '&lt;/li&gt;';
}


Also, I'm using Panels, Panels Everywhere, Page Manager, and MiniPanels — this could inform the ultimate solution.

Thanks all.
You need to use hook_menu_alter.

Make sure you declare the function with the &amp; in front of items argument, this is what allows the function to modify the $items array passed to the function:

function MYMODULE_menu_alter(&amp;$items)


In your function you can access every menu item in an array structure like so:

   $items['node/%/edit/schedulenew'] = array(
            'title' =&gt; 'Schedule1',
            'access callback'=&gt;'user_access',
            'access arguments'=&gt;array('view cssswitch'),
            'page callback' =&gt; 'cssswitch_schedule',
            'page arguments' =&gt; array(1),
            'type' =&gt; MENU_LOCAL_TASK,
            'weight'=&gt;4,
        );


If you just redefine the 'weight' property, it will allow you to re-order your menus.


Be sure to clear your cache after each menu changes, or go to the menu page, load the menu edit page and save it, this also rebuilds the menu.