# Alphordering Numbers

#### 2019-09-12

## Numbers don’t comply with alphabetical order

`poorly_ordered` is a vector of file names of images of patient samples from different days.

    poorly_ordered
    #>  [1] "patient101-day9.png"  "patient98-day10.png"  "patient98-day9.png"
    #>  [4] "patient99-day9.png"   "patient98-day11.png"  "patient102-day11.png"
    #>  [7] "patient102-day10.png" "patient100-day11.png" "patient100-day9.png"
    #> [10] "patient101-day11.png" "patient102-day9.png"  "patient99-day10.png"
    #> [13] "patient103-day10.png" "patient103-day9.png"  "patient101-day10.png"
    #> [16] "patient100-day10.png" "patient103-day11.png" "patient99-day11.png"

How do we get this vector into order? Well, alphabetical order doesn’t work:

    sort(poorly_ordered)
    #>  [1] "patient100-day10.png" "patient100-day11.png" "patient100-day9.png"
    #>  [4] "patient101-day10.png" "patient101-day11.png" "patient101-day9.png"
    #>  [7] "patient102-day10.png" "patient102-day11.png" "patient102-day9.png"
    #> [10] "patient103-day10.png" "patient103-day11.png" "patient103-day9.png"
    #> [13] "patient98-day10.png"  "patient98-day11.png"  "patient98-day9.png"
    #> [16] "patient99-day10.png"  "patient99-day11.png"  "patient99-day9.png"

Patient 100 comes before patient 99. This is because 1 comes before 9 in alphabetical order.

## Alphordering numbers

It’s possible to alphorder the numbers by prefixing them with zeroes:

    strex::str_alphord_nums(poorly_ordered)
    #>  [1] "patient101-day09.png" "patient098-day10.png" "patient098-day09.png"
    #>  [4] "patient099-day09.png" "patient098-day11.png" "patient102-day11.png"
    #>  [7] "patient102-day10.png" "patient100-day11.png" "patient100-day09.png"
    #> [10] "patient101-day11.png" "patient102-day09.png" "patient099-day10.png"
    #> [13] "patient103-day10.png" "patient103-day09.png" "patient101-day10.png"
    #> [16] "patient100-day10.png" "patient103-day11.png" "patient099-day11.png"

Having done this, the alphabetical order is the one we want:

    sort(strex::str_alphord_nums(poorly_ordered))
    #>  [1] "patient098-day09.png" "patient098-day10.png" "patient098-day11.png"
    #>  [4] "patient099-day09.png" "patient099-day10.png" "patient099-day11.png"
    #>  [7] "patient100-day09.png" "patient100-day10.png" "patient100-day11.png"
    #> [10] "patient101-day09.png" "patient101-day10.png" "patient101-day11.png"
    #> [13] "patient102-day09.png" "patient102-day10.png" "patient102-day11.png"
    #> [16] "patient103-day09.png" "patient103-day10.png" "patient103-day11.png"