this is my code:

    records.par_iter().for_each(|(chrom, lines)| {
        let mut layer: Vec<(u32, Arc<str>, Arc<str>)> = Vec::new();
        let mut mapper: HashMap<Arc<str>, Vec<Arc<str>>> = HashMap::new();
        let mut inner: HashMap<Arc<str>, BTreeMap<Sort, String>> = HashMap::new();
        let mut helper: HashMap<Arc<str>, Arc<str>> = HashMap::new();

        for line in lines {
            match line.feat.as_str() {
                "gene" => {
                    layer.push(line.outer_layer());
                }
                "transcript" => {
                    mapper
                        .entry(line.gene_id.clone())
                        .or_default()
                        .push(line.transcript_id.clone());
                    helper
                        .entry(line.transcript_id.clone())
                        .or_insert(line.line.clone());
                }
                "CDS" | "exon" | "start_codon" | "stop_codon" => {
                    inner.entry(line.transcript_id.clone()).or_default().insert(
                        Sort::new(line.exon_number.clone().as_str()),
                        line.line.clone().to_string(),
                    );
                }
                _ => {
                    inner
                        .entry(line.transcript_id.clone())
                        .or_default()
                        .entry(Sort::new(line.feat.clone().as_str()))
                        .and_modify(|e| {
                            e.push('\n');
                            e.push_str(&line.line.clone());
                        })
                        .or_insert(line.line.clone().to_string());
                }
            }
        }

        layer.par_sort_unstable_by_key(|x| x.0);
    });

as you can see, I am doing some things in parallel (per each chromosome), the problem now is that I need to write things to a file, but the things need to be in order (chromosome order). This was the code I used in my previous version (which was not parallel):

 //
    // layer.par_sort_unstable_by(|a, b| {
    //     let cmp_chr = compare(&a.0, &b.0);
    //     if cmp_chr == std::cmp::Ordering::Equal {
    //         a.1.cmp(&b.1)
    //     } else {
    //         cmp_chr
    //     }
    // });
    //
    // for i in layer {
    //     writeln!(output, "{}", i.3).unwrap();
    //
    //     let transcripts = mapper
    //         .get(&i.2)
    //         .ok_or("Error: genes with 0 transcripts are not allowed")
    //         .unwrap();
    //     for j in transcripts.iter() {
    //         writeln!(output, "{}", helper.get(j).unwrap()).unwrap();
    //         let exons = inner
    //             .get(j)
    //             .ok_or("Error: transcripts with 0 exons are not allowed")
    //             .unwrap();
    //         let joined_exons: String = exons
    //             .values()
    //             .map(|value| value.to_string())
    //             .collect::<Vec<String>>()
    //             .join("\n");
    //         writeln!(output, "{}", joined_exons).unwrap();
    //     }
    // }

Since things were not in parallel, I would have a only "layer" that I could sort and then iterate over to write things in order. Any idea on how to do the same thing without losing speed (which is the main goal here, make it a lot faster).