def sort_stats(stats, sortedby=None, tree=False, reverse=True):
 if (sortedby is None):
  return stats
 if ((sortedby == 'io_counters') and (not tree)):
  try:
   stats.sort(key=(lambda process: (((process[sortedby][0] - process[sortedby][2]) + process[sortedby][1]) - process[sortedby][3])), reverse=reverse)
  except Exception:
   stats.sort(key=operator.itemgetter('cpu_percent'), reverse=reverse)
 elif tree:
  stats.set_sorting(sortedby, reverse)
 else:
  try:
   stats.sort(key=operator.itemgetter(sortedby), reverse=reverse)
  except (KeyError, TypeError):
   stats.sort(key=operator.itemgetter('name'), reverse=False)
 return stats

def sort_stats ( stats , sortedby = None , tree = False , reverse = True ) : if ( sortedby is None ) : return stats if ( ( sortedby == 'io_counters' ) and ( not tree ) ) : try : stats . sort ( key = ( lambda process : ( ( ( process [ sortedby ] [ 0 ] - process [ sortedby ] [ 2 ] ) + process [ sortedby ] [ 1 ] ) - process [ sortedby ] [ 3 ] ) ) , reverse = reverse ) except Exception : stats . sort ( key = operator . itemgetter ( 'cpu_percent' ) , reverse = reverse ) elif tree : stats . set_sorting ( sortedby , reverse ) else : try : stats . sort ( key = operator . itemgetter ( sortedby ) , reverse = reverse ) except ( KeyError , TypeError ) : stats . sort ( key = operator . itemgetter ( 'name' ) , reverse = False ) return stats
return the stats sorted by reverse the sort if reverse is true .