#sorting #numbers

bin+lib primo

Sort a file, correctly handling multi-digits numbers

1 unstable release

Uses old Rust 2015

0.0.1 May 8, 2017

#27 in #sorted

MIT license

6KB
87 lines

Primo

primo is a library and command-line tool to sort files like UNIX’s sort. Unlike sort, however, it interprets series of digits as full numbers:

# input
I have 9 apples.
I have 42 apples.
I have 5 apples.


# sort
I have 42 apples.
I have 5 apples.
I have 9 apples.

# primo
I have 5 apples.
I have 9 apples.
I have 42 apples.

Note this is my first ever Rust program so the code might not be the best.

Usage

Command-Line

primo [<filename>]

It reads on stdin or from the provided file and print the sorted version on stdout.

Library

extern crate primo;

fn main() {
    let mut lines = vec![
        "my 1st line".to_string(),
        "...".to_string(),
        "my 15th line".to_string(),
        "my 2nd line".to_string(),
    ];

    primo::sort_vec(&mut lines);

    // prints:
    //    ...
    //    my 1st line
    //    my 2nd line
    //    my 15th line
    //
    for line in lines {
        println!("{}", line);
    }
}

Known Issues

  • The sort is quite slow for now because the parsing function is called multiple times on each string
  • Chars are treated as numbers, so "abc" will sort after "25bc" and before "27bc" because 'a'’s i32 value is 26.

FAQ

What about sort’s -V option?

The GNU coreutils package has a sort implementation that supports primo’s main use-case with its -V option. I unfortunately learnt about this option after writing primo.

Dependencies

~1.5MB
~23K SLoC