title: Sort a PySide.QtGui.QTreeWidget by an alpha numeric column
body: <p>I would like to sort a <code>QTreeWidget</code> by the <code>alpha_numeric</code> column.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Following <a href="https://stackoverflow.com/a/21032644/1424995">ekhumoro's example</a>, I re-implemented the <code>QTreeWidgetItem</code> <code>__lt__</code> method to use the <a href="https://stackoverflow.com/questions/5254021/python-human-sort-of-numbers-with-alpha-numeric-but-in-pyqt-and-a-lt-oper">desired natural sorting</a>.</p>
  
  <p>Relevant code:</p>
  
  <pre><code>class TreeWidgetItem(QtGui.QTreeWidgetItem):
      def __lt__(self, other):
          column = self.treeWidget().sortColumn()
          key1 = self.text(column)
          key2 = other.text(column)
          return self.natural_sort_key(key1) &lt; self.natural_sort_key(key2)
  
      @staticmethod
      def natural_sort_key(key):
          regex = '(\d*\.\d+|\d+)'
          parts = re.split(regex, key)
          return tuple((e if i % 2 == 0 else float(e)) for i, e in enumerate(parts))
  </code></pre>
  
  <p><strong>Updated full working example:</strong></p>
  
  <pre><code>import sys
  import re
  from PySide import QtGui, QtCore
  
  data = [
      {"name": "Apple", "alpha_numeric": "11"},
      {"name": "Apple", "alpha_numeric": "125b"},
      {"name": "Apple", "alpha_numeric": "125a"},
      {"name": "Apple", "alpha_numeric": "3"},
      {"name": "Orange", "alpha_numeric": "11"},
      {"name": "Orange", "alpha_numeric": "125b"},
      {"name": "Orange", "alpha_numeric": "125a"},
      {"name": "Orange", "alpha_numeric": "3"},
  ]
  
  # re-implement the QTreeWidgetItem
  class TreeWidgetItem(QtGui.QTreeWidgetItem):
      def __lt__(self, other):
          column = self.treeWidget().sortColumn()
          key1 = self.text(column)
          key2 = other.text(column)
          return self.natural_sort_key(key1) &lt; self.natural_sort_key(key2)
  
      @staticmethod
      def natural_sort_key(key):
          regex = '(\d*\.\d+|\d+)'
          parts = re.split(regex, key)
          return tuple((e if i % 2 == 0 else float(e)) for i, e in enumerate(parts))
  
  
  class MainWindow(QtGui.QMainWindow):
      def __init__(self):
          super(MainWindow, self).__init__()
          self.resize(300, 300)
          self.table = None
          self.ui()
  
      def ui(self):
          # setup the table
          self.table = QtGui.QTreeWidget()
          self.table.setRootIsDecorated(False)
          self.table.setAlternatingRowColors(True)
          self.table.setSortingEnabled(True)
  
          # columns and widths
          column_names = ['Name', 'Alpha Numeric']
          self.table.setColumnCount(2)
          self.table.setColumnWidth(0, 150)
          self.table.setColumnWidth(1, 150)
          self.table.setHeaderLabels(column_names)
  
          # row data
          for row in data:
              column = TreeWidgetItem(self.table)
              column.setSizeHint(0, QtCore.QSize(30, 30))
              column.setText(0, row['name'])
              column.setText(1, row['alpha_numeric'])
  
          # sort
          self.table.sortByColumn(1, QtCore.Qt.AscendingOrder)
          self.table.sortByColumn(0, QtCore.Qt.AscendingOrder)
  
          # setup the layout
          self.setCentralWidget(self.table)
  
  
  if __name__ == '__main__':
      app = QtGui.QApplication(sys.argv)
      window = MainWindow()
      window.show()
      sys.exit(app.exec_())
  </code></pre>