Ordering factors in data table using DT package
<p>I have data that I want to display in 5% increments, such as 5-10%, 10-15%, etc.  To do this, I have a data frame that stores them as a factor, with the levels being the midpoint of the range, and the label being the range to display.  For example, the level 12.5 would be labeled 10-15%. </p>

<p>However, I'm having trouble sorting this correctly using a datatable.</p>

<pre><code>library('DT')
example &lt;- data.frame(name = c('A', 'B', 'C', 'D'),
                  value = factor(c(7.5, 12.5, 7.5, 17.5),
                                  levels = c(7.5, 12.5, 17.5),
                                  labels = c('5-10%', '10-15%', '15-20%')))

datatable(example,
      rownames = FALSE,
      options = list(order = list(1, 'asc')))
</code></pre>

<p><a href="https://i.stack.imgur.com/CUcSV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CUcSV.png" alt="enter image description here"></a>
<br></p>

<p>As you can see, it appears to be sorting off of the first number of the character string, rather than sorting by the level of the factor.  </p>

<p>Any ideas on how I can get the data table to sort off of the factor levels, rather than the character string?  (Other than ordering the data frame before passing it into the data table - I would like this to be correctly sortable in either direction by clicking the sort arrow)</p>