     }
 }
 
+(int keys[]) sort_keys(string params[][]) {
+  sort_code =
+"""
+key_string = '%s'
+keys = [int(x) for x in key_string.split(',')]
+keys.sort()
+result = ",".join([str(x) for x in keys])
+""";
+
+  string k[] = keys_string(params);
+  string code = sort_code % (join(k, ","));
+  result = python_persist(code, "result");
+  foreach val, i in split(result, ",")
+  {
+    keys[i] = string2int(val);
+  }
+}
+
 main() {
   string params[][];
-  foreach study1 in studies
+  
+  foreach study1, i in studies
   {
     foreach study2 in studies
     {
-
       if (study1 != study2)
       {
         foreach cutoff in cutoffs

                gpus, feature_file, cache_dir);
            updated_param = python_persist(param_code, "params_json");
            // TODO DB: insert updated_param with mlr_instance_id and record 
            printf("Updated Params: %s", updated_param);
            //results[j] = "0.5";
            results[j] = obj_prio(updated_param,
                             "%00i_%00i_%000i_%0000i" % (mlr_instance_id, restart_number,i,j), prio);

          printf(fname);

          string record_id = compute_feature_correlation(study1, study2, cutoff[0], cutoff[1], fname);
          int h = string2int(record_id);
          params[h] = [record_id, fname, study1];
        }
      }
    }

     // for each of the studies, run against full features, no xcorr
    string log_code = log_corr_template % (db_file, "", study1, "", -1, -1);
    string record_id = python_persist(log_code, "str(record_id)");
    // give full features key value guaranteed to be lower than the record_ids 
    // of the xcorr studies
    params[-(i + 1)] = [record_id, "", study1];
  }

  int ME_ranks[];
