title: save row number in core data and order by desc
body: <p>I am working with swift 4 for macOS and I get my data from core data into a NSTableview.</p>
  
  <p>Now I would like to save the order of my data.
  Example:</p>
  
  <pre><code>MyCoreData1 (Row 1)
  MyCoreData2 (Row 2)
  MyCoreData3 (Row 3)
  MyCoreData4 (Row 4)
  </code></pre>
  
  <p>Now I sort it with drag and drop like this:</p>
  
  <pre><code>MyCoreData1 (Row 3)
  MyCoreData2 (Row 2)
  MyCoreData3 (Row 1)
  MyCoreData4 (Row 4)
  </code></pre>
  
  <p>How can I save this new order into core data?
  At the moment I have an attribute "sortNumber" in my entity </p>
  
  <pre><code>MyCoreData1 (sortNumber 10)
  MyCoreData2 (sortNumber 20)
  MyCoreData3 (sortNumber 30)
  MyCoreData4 (sortNumber 40)
  </code></pre>
  
  <p>and I will sort it by sortNumber "Desc"
  But if I re-sort it, I have to build a complex (I guess it will be complex) logic to change the sortNumber of each element to the new sort order.</p>
  
  <p>Have you any ideas how I can solve this?</p>
  
  <p><strong>Code for Drag a row</strong></p>
  
  <pre><code>func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -&gt; Bool {
          let pasteboard = info.draggingPasteboard()
          let pasteboardData = pasteboard.data(forType: NSPasteboard.PasteboardType.string)
  
          if let pasteboardData = pasteboardData {
  
              if let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: pasteboardData) as? IndexSet {
                  var oldIndexOffset = 0
                  var newIndexOffset = 0
  
                  for oldIndex in rowIndexes {
  
                      if oldIndex &lt; row {
                          tableView.moveRow(at: oldIndex + oldIndexOffset, to: row - 1)
                          oldIndexOffset -= 1
                      } else {
                          tableView.moveRow(at: oldIndex, to: row + newIndexOffset)
                          newIndexOffset += 1
                      }
                  }
              }
          }
  
          return true
      }
  </code></pre>