fn filename_comparator(file1: &PathBuf, file2: &PathBuf) -> Ordering {
    let name1 = file1.file_name().unwrap().to_str().unwrap();
    let name2 = file2.file_name().unwrap().to_str().unwrap();
    let a1 = name1.split(".").collect::<Vec<&str>>();
    let a2 = name2.split(".").collect::<Vec<&str>>();
    let mut date_str1 = a1[2];
    let mut date_str2 = a2[2];

    // in case of file name contains pid, skip it, like Sentinel-Admin-metrics.log.pid22568.2018-12-24
    if a1[2].starts_with(FILE_PID_PREFIX) {
        date_str1 = a1[3];
        date_str2 = a2[3];
    }

    // compare date first
    if date_str1 != date_str2 {
        return date_str1.cmp(&date_str2);
    }

    // same date, compare the file number
    name1.cmp(&name2)
}
/// Sort the metric files by their date time.
/// This function is used to remove the deprecated files, or create a new file in order.
Sort the metric files by their date time.
This function is used to remove the deprecated files, or create a new file in order.
Sort the metric files by their date time . This function is used to remove the deprecated files or create a new file in order .
fn filename_comparator(file1: &PathBuf, file2: &PathBuf) -> Ordering {
    let name1 = file1.file_name().unwrap().to_str().unwrap();
    let name2 = file2.file_name().unwrap().to_str().unwrap();
    let a1 = name1.split(".").collect::<Vec<&str>>();
    let a2 = name2.split(".").collect::<Vec<&str>>();
    let mut date_str1 = a1[2];
    let mut date_str2 = a2[2];
    // in case of file name contains pid, skip it, like Sentinel-Admin-metrics.log.pid22568.2018-12-24
    if a1[2].starts_with(FILE_PID_PREFIX) {
        date_str1 = a1[3];
        date_str2 = a2[3];
    }
    // compare date first
    if date_str1 != date_str2 {
        return date_str1.cmp(&date_str2);
    }
    // same date, compare the file number
    name1.cmp(&name2)
}
fn filename_comparator ( file1 : & PathBuf , file2 : & PathBuf ) -> Ordering { let name1 = file1 . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ; let name2 = file2 . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ; let a1 = name1 . split ( " " ) . collect :: < Vec < & str > > ( ) ; let a2 = name2 . split ( " " ) . collect :: < Vec < & str > > ( ) ; let mut date_str1 = a1 [ 2 ] ; let mut date_str2 = a2 [ 2 ] ; // in case of file name contains pid, skip it, like Sentinel-Admin-metrics.log.pid22568.2018-12-24 if a1 [ 2 ] . starts_with ( FILE_PID_PREFIX ) { date_str1 = a1 [ 3 ] ; date_str2 = a2 [ 3 ] ; } // compare date first if date_str1 != date_str2 { return date_str1 . cmp ( & date_str2 ) ; } // same date, compare the file number name1 . cmp ( & name2 ) }
Sort the metric files by their date time.
Sort the metric files by their date time .
file1
&PathBuf
file2
&PathBuf
file1
&PathBuf
file2
&PathBuf