def reverse_order(order):
 items = []
 for item in order.split(','):
  item = item.lower().split()
  direction = ('asc' if (item[1:] == ['desc']) else 'desc')
  items.append(('%s %s' % (item[0], direction)))
 return ', '.join(items)

def reverse_order ( order ) : items = [ ] for item in order . split ( ',' ) : item = item . lower ( ) . split ( ) direction = ( 'asc' if ( item [ 1 : ] == [ 'desc' ] ) else 'desc' ) items . append ( ( '%s %s' % ( item [ 0 ] , direction ) ) ) return ', ' . join ( items )
reverse an order by clause .