def sort(filename, key, outputFile, fields=None, watermark=((1024 * 1024) * 100)):
 if (fields is not None):
  assert set(key).issubset(set([f[0] for f in fields]))
 with FileRecordStream(filename) as f:
  if fields:
   fieldNames = [ff[0] for ff in fields]
   indices = [f.getFieldNames().index(name) for name in fieldNames]
   assert (len(indices) == len(fields))
  else:
   fileds = f.getFields()
   fieldNames = f.getFieldNames()
   indices = None
  key = [fieldNames.index(name) for name in key]
  chunk = 0
  records = []
  for (i, r) in enumerate(f):
   if indices:
    temp = []
    for i in indices:
     temp.append(r[i])
    r = temp
   records.append(r)
   available_memory = psutil.avail_phymem()
   if (available_memory < watermark):
    _sortChunk(records, key, chunk, fields)
    records = []
    chunk += 1
  if (len(records) > 0):
   _sortChunk(records, key, chunk, fields)
   chunk += 1
  _mergeFiles(key, chunk, outputFile, fields)

def sort ( filename , key , outputFile , fields = None , watermark = ( ( 1024 * 1024 ) * 100 ) ) : if ( fields is not None ) : assert set ( key ) . issubset ( set ( [ f [ 0 ] for f in fields ] ) ) with FileRecordStream ( filename ) as f : if fields : fieldNames = [ ff [ 0 ] for ff in fields ] indices = [ f . getFieldNames ( ) . index ( name ) for name in fieldNames ] assert ( len ( indices ) == len ( fields ) ) else : fileds = f . getFields ( ) fieldNames = f . getFieldNames ( ) indices = None key = [ fieldNames . index ( name ) for name in key ] chunk = 0 records = [ ] for ( i , r ) in enumerate ( f ) : if indices : temp = [ ] for i in indices : temp . append ( r [ i ] ) r = temp records . append ( r ) available_memory = psutil . avail_phymem ( ) if ( available_memory < watermark ) : _sortChunk ( records , key , chunk , fields ) records = [ ] chunk += 1 if ( len ( records ) > 0 ) : _sortChunk ( records , key , chunk , fields ) chunk += 1 _mergeFiles ( key , chunk , outputFile , fields )
takes a list of integers and sorts them in ascending order .