title: How to Sort Listview Columns Containing Strings & Integers?
body: <h2>General Information</h2>
  <p>I have a ListView containing multiple columns. I have used a <a href="http://msdn.microsoft.com/en-us/library/ms229643%28v=vs.80%29.aspx" rel="nofollow noreferrer">Microsoft Method to sort my ListView Columns</a>. The ListView  is populated by a SQL Query which correctly sorts Strings and Integers together (<em>code shown below</em>). For Example:</p>
  <p><strong>String &amp; Integer Sorting Problem</strong></p>
  <p>The following <code>JobNumber</code> <strong>strings</strong> are considered as sorted</p>
  <p><em>&quot;10&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;</em></p>
  <p>Using my SQL Query, they will become</p>
  <p><em>&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;10&quot;</em></p>
  <hr />
  <h2>SQL String &amp; Integer Sorter</h2>
  <p>Here is the Query I use to sort Strings and Integers correctly</p>
  <pre><code>SELECT PK_BillHeader, JobNumber, Description
  FROM dbo.BillHeaders 
  ORDER BY Case IsNumeric(JobNumber) 
      When 1 Then Replicate('0', 50 - Len(JobNumber)) + JobNumber 
          Else JobNumber 
      End
  </code></pre>
  <p>This enters zeros until it gets the maximum length of my SQL column (50 chars) minus the <code>JobNumber</code>'s current length. That way, everything is considered as a string (including integers) which can then be sorted out correctly.</p>
  <hr />
  <h2>My Problem</h2>
  <p>When I click on the ListView Column Header (causing it to sort), it stops sorting the strings and integers correctly as it did with my SQL query. Instead, it sorts everything as a string which replicates my &quot;<em>String &amp; Integer</em>&quot; sort problem once more ... What is more, if I have decimal values (<em>second picture</em>), it sorts them out pretty weird (<em>see my compare code</em>)</p>
  <p><img src="https://i.stack.imgur.com/NjVqO.png" alt="Machine Number String &amp; Integer Problem" /> <img src="https://i.stack.imgur.com/yZYyn.png" alt="Money Sort" /></p>
  <p><strong>Compare Code</strong></p>
  <pre><code>Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
          Dim xItem As clsSortWrapper = CType(x, clsSortWrapper)
          Dim yItem As clsSortWrapper = CType(y, clsSortWrapper)
  
          Dim xText As String = xItem.sortItem.SubItems(xItem.sortColumn).Text
          Dim yText As String = yItem.sortItem.SubItems(yItem.sortColumn).Text
  
          If Decimal.TryParse(xText, vbNull) Then xText = xText.PadLeft(10, &quot;0&quot;c)
          If Decimal.TryParse(yText, vbNull) Then yText = yText.PadLeft(10, &quot;0&quot;c)
          Return xText.CompareTo(yText) * IIf(Me.ascending, 1, -1)
      End Function
  </code></pre>
  <p>Is there a simpler way to acheive my result of sorting a ListView with Strings and Integers?</p>
  <hr />
  <p><em>Note:</em></p>
  <p>I omitted posting the <a href="http://msdn.microsoft.com/en-us/library/ms229643%28v=vs.80%29.aspx" rel="nofollow noreferrer">ListView Sort code</a> here because it would've clustered the post quite a bit. I provided the link which explains it thoroughly.</p>