title: Sorting Rows in Excel with POI
body: <p>I'm trying to sort an XSSF sheet in POI and I'm not having any luck. I found a prior post saying the issue with shifting rows was fixed in 3.9 but I'm still getting an error. I tried using the method below and this is the error I'm getting..</p>
  
  <pre><code>   /**
   * Sorts (A-Z) rows by String column
   * @param sheet - sheet to sort
   * @param column - String column to sort by
   * @param rowStart - sorting from this row down
   */
  private void sortSheet(Sheet sheet, int column, int rowStart) {
      boolean sorting = true;
      int lastRow = sheet.getLastRowNum();
      while (sorting == true) {
          sorting = false;
          for (Row row : sheet) {
              // skip if this row is before first to sort
              if (row.getRowNum()&lt;rowStart) continue;
              // end if this is last row
              if (lastRow==row.getRowNum()) break;
              Row row2 = sheet.getRow(row.getRowNum()+1);
              if (row2 == null) continue;
              String firstValue = (row.getCell(column) != null) ? row.getCell(column).getStringCellValue() : "";
              String secondValue = (row2.getCell(column) != null) ? row2.getCell(column).getStringCellValue() : "";
              //compare cell from current row and next row - and switch if secondValue should be before first
              if (secondValue.compareToIgnoreCase(firstValue)&lt;0) {                    
                  sheet.shiftRows(row2.getRowNum(), row2.getRowNum(), -1);
                  sheet.shiftRows(row.getRowNum(), row.getRowNum(), 1);
                  sorting = true;
              }
  
          }
      }
  
  
  Exception in thread "AWT-EventQueue-0" org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
      at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
      at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl.getR(Unknown Source)
      at org.apache.poi.xssf.usermodel.XSSFRow.getRowNum(XSSFRow.java:322)
      at Objects.Event.sortSheet(Event.java:426)
      at Objects.Event.writeRigSheetsAverages(Event.java:394)
      at Objects.GroupInfo.seperateRigTypes(GroupInfo.java:138)
      at Objects.GroupInfo.&lt;init&gt;(GroupInfo.java:46)
      at DowntimeReports.StartProgram(DowntimeReports.java:135)
      at DowntimeReports.&lt;init&gt;(DowntimeReports.java:52)
      at DowntimeGUI.actionPerformed(DowntimeGUI.java:89)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$200(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>
  
  <p>My sheet starts with 5 rows but when I make the shift it gets reset to 3. </p>