about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi <fredmanglis@gmail.com>2022-09-30 09:17:51 +0300
committerFrederick Muriuki Muriithi <fredmanglis@gmail.com>2022-09-30 09:17:51 +0300
commitfd85c0f75e03f170b97fcedba26317e0832d087e (patch)
tree4b343e75846c560d49817004d498b5f53db688e3 /src
parent6504956b28e3b6c56780845d0170490ae82e1aa6 (diff)
downloadcorrelation_rust-fd85c0f75e03f170b97fcedba26317e0832d087e.tar.gz
Handle "divide-by-zero"
Sort NAN as less than everything else
Diffstat (limited to 'src')
-rw-r--r--src/correlations.rs6
-rw-r--r--src/sorter.rs3
2 files changed, 7 insertions, 2 deletions
diff --git a/src/correlations.rs b/src/correlations.rs
index 6b38ffe..7c79c7e 100644
--- a/src/correlations.rs
+++ b/src/correlations.rs
@@ -1,3 +1,5 @@
+use std::cmp::Ordering;
+
 use crate::parser::parse_rows_with_names;
 use crate::reader::BufferReader;
 use crate::sorter::sort_write_to_file;
@@ -171,7 +173,9 @@ impl<'a> Compute<'a> {
 
         //naive implementation try extern sorting could save 3 seconds
 
-        corr_results.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap());
+        corr_results.sort_by(|a, b| {
+	    b.1.abs().partial_cmp(&a.1.abs()).unwrap_or_else(|| {
+		Ordering::Less})});
 
         sort_write_to_file(String::from(self.output_file), corr_results)
     }
diff --git a/src/sorter.rs b/src/sorter.rs
index af9c419..fd88e9c 100644
--- a/src/sorter.rs
+++ b/src/sorter.rs
@@ -2,6 +2,7 @@
 
 //https://betterprogramming.pub/how-to-sort-a-20g-file-in-rust-12abfffbd92b
 use std::fs::File;
+use std::cmp::Ordering;
 use std::io::{BufWriter, Write};
 //use extsort::*;
 
@@ -50,7 +51,7 @@ pub fn sort_write_to_file(
 ) -> std::io::Result<String> {
     File::create(filename.clone())?;
 
-    v.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap());
+    v.sort_by(|a, b| b.1.abs().partial_cmp(&a.1.abs()).unwrap_or_else(|| Ordering::Less));
     let mut buffer = BufWriter::with_capacity(BUFFER_CAPACITY, File::create(&filename).unwrap());
     for (name, rho, p_val, num_overlap) in v.iter() {
         writeln!(buffer, "{},{},{},{}", name, rho, p_val, num_overlap).unwrap();