title: Sorting rows based on variable group in a data frame
body: <p>I've got a dataframe that looks like this -</p>
  
  <pre><code>df=data.frame(Code=c('Q1','Q1','Q1','Q1','Q2','Q2','Q2','Q2','Q3','Q3','Q3','Q3'),
            Fiscal_Year=c('FY18','FY16','FY17','FY15','FY15','FY18','FY17','FY16','FY15','FY16','FY17','FY18'),
            Score=c(0.23,0.25,0.32,0.41,0.61,0.54,0.45,0.51,0.78,0.79,0.81,0.84))
  </code></pre>
  
  <p>My objective is to group by question <code>Code</code> (which is already the case in the dataframe), and then for each group, sort rows in increasing order of <code>Fiscal_Year</code>.</p>
  
  <p>The fiscal years are FY15 (Fiscal Year 2015), FY16 (Fiscal Year 2016), FY17 (Fiscal Year 2017), &amp; FY18 (Fiscal Year 2018).</p>
  
  <blockquote>
    <p>So the order of levels are FY15 &lt; FY16 &lt; FY17 &lt; FY18.</p>
  </blockquote>
  
  <p>In order to define the levels in R, I did the following -  </p>
  
  <pre><code>#Convert column to factor
  df$Fiscal_Year=as.factor(df$Fiscal_Year)
  #Define order of levels
  levels(df$Fiscal_Year)=c("FY15","FY16","FY17","FY18")
  </code></pre>
  
  <p>Next, I wish to group by <code>Code</code> and then <strong>sort in increasing order</strong> of <code>Fiscal_Year</code> <strong>within each group</strong>.</p>
  
  <p>So for example, for the question group <code>Q2</code>, I'd like the rows to be sorted in increasing order of Fiscal Year (FY15,FY16,FY17,FY18). And the same for the other question groups.</p>
  
  <p><strong>My Attempt</strong></p>
  
  <p>Using the dplyr package, I tried to do this</p>
  
  <pre><code>library(dplyr)
  
  df=sat %&gt;%
  group_by(CTQ_QUEST_CODE,FY) %&gt;%
  arrange(CTQ_QUEST_CODE,desc(FY))
  </code></pre>
  
  <p>However I do not get the result I'm looking for.</p>
  
  <p>Any inputs on this would be highly appreciated.</p>