title: How to sort a tibble by column order? (first column, second column, third...)
body: <p>I have a tibble that contains an arbitrary number of columns.
  These columns were inserted in order so their index (first, second, ...) is meaningful.</p>
  <p>I'm trying to sort the tibble by the first column, then the second column, then the third, and so on.</p>
  <p>I'd rather keep using <code>dplyr::arrange()</code> to be consistent with my framework, but if it cannot be done I'd gladly accept any solution.</p>
  <p>Also, if missing values could be considered last that would be a great plus.</p>
  <p>Here is a reproducible example with my expected output and some failed attempts:</p>
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  set.seed(0)
  x=as_tibble(mtcars)[1:4] %&gt;% sample_n(5)
  x
  #&gt; # A tibble: 5 x 4
  #&gt;     mpg   cyl  disp    hp
  #&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  #&gt; 1  15.2     8  276.   180
  #&gt; 2  19.2     8  400    175
  #&gt; 3  21.4     6  258    110
  #&gt; 4  14.3     8  360    245
  #&gt; 5  21       6  160    110
  
  # **** EXPECTED OUTPUTS: ****
  
  arrange(x, mpg, cyl, disp, hp)
  #&gt; # A tibble: 5 x 4
  #&gt;     mpg   cyl  disp    hp
  #&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  #&gt; 1  14.3     8  360    245
  #&gt; 2  15.2     8  276.   180
  #&gt; 3  19.2     8  400    175
  #&gt; 4  21       6  160    110
  #&gt; 5  21.4     6  258    110
  arrange(x, hp, disp, cyl, mpg)
  #&gt; # A tibble: 5 x 4
  #&gt;     mpg   cyl  disp    hp
  #&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  #&gt; 1  21       6  160    110
  #&gt; 2  21.4     6  258    110
  #&gt; 3  19.2     8  400    175
  #&gt; 4  15.2     8  276.   180
  #&gt; 5  14.3     8  360    245
  
  # **** MY FAILED ATTEMPTS: ****
  
  arrange(x, all_of(colnames(x)))
  #&gt; Error: arrange() failed at implicit mutate() step. 
  #&gt; * Problem with `mutate()` column `..1`.
  #&gt; i `..1 = all_of(colnames(x))`.
  #&gt; i `..1` must be size 5 or 1, not 4.
  arrange(x, !!all_of(colnames(x)))
  #&gt; Error: arrange() failed at implicit mutate() step. 
  #&gt; * Problem with `mutate()` column `..1`.
  #&gt; i `..1 = &lt;chr&gt;`.
  #&gt; i `..1` must be size 5 or 1, not 4.
  arrange(x, !!!all_of(names(x)))
  #&gt; # A tibble: 5 x 4
  #&gt;     mpg   cyl  disp    hp
  #&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  #&gt; 1  15.2     8  276.   180
  #&gt; 2  19.2     8  400    175
  #&gt; 3  21.4     6  258    110
  #&gt; 4  14.3     8  360    245
  #&gt; 5  21       6  160    110
  do.call(arrange, x, colnames(x))
  #&gt; Warning in if (quote) args &lt;- lapply(args, enquote): the condition has length &gt;
  #&gt; 1 and only the first element will be used
  #&gt; Error in if (quote) args &lt;- lapply(args, enquote): argument is not interpretable as logical
  do.call(arrange, x, list(colnames(x)))
  #&gt; Error in if (quote) args &lt;- lapply(args, enquote): argument is not interpretable as logical
  </code></pre>
  <p><sup>Created on 2021-08-10 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.0)</sup></p>