如何排序列表视图的列含有字符串的&安培;整型? [英] How to Sort Listview Columns Containing Strings & Integers?

查看:93
本文介绍了如何排序列表视图的列含有字符串的&安培;整型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个列一个ListView。我使用了一个微软方法我的ListView列排序。 ListView控件是由一个SQL查询其正确排序字符串和整数一起填充( code如下所示的)。例如:

I have a ListView containing multiple columns. I have used a Microsoft Method to sort my ListView Columns. The ListView is populated by a SQL Query which correctly sorts Strings and Integers together (code shown below). For Example:

字符串和放大器;整数排序问题

下面的 jobnumber可以 字符串被视为分类

10,1,2,3

使用我的SQL查询,他们将成为

Using my SQL Query, they will become

1,2,3,10

下面是查询我使用的字符串和整数正确排序

Here is the Query I use to sort Strings and Integers correctly

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

进入零,直到它得到我的SQL列(50个字符)减去 jobnumber可以的当前长度的最大长度。这样一来,所有被认为是一个字符串(包括整数),然后可以正确地选出来。

This enters zeros until it gets the maximum length of my SQL column (50 chars) minus the JobNumber's current length. That way, everything is considered as a string (including integers) which can then be sorted out correctly.

当我点击的ListView列标题(使其排序),将停止正确排序字符串和整数,因为它与我的SQL查询一样。相反,它的一切排序作为复制我的一个字符串的字符串和放大器;整数的之类的问题,再一次......更重要的是,如果我有十进制值(第二张图片的),它梳理出来pretty怪异(看我比较code 的)

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 "String & Integer" sort problem once more ... What is more, if I have decimal values (second picture), it sorts them out pretty weird (see my compare code)

比较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, "0"c)
        If Decimal.TryParse(yText, vbNull) Then yText = yText.PadLeft(10, "0"c)
        Return xText.CompareTo(yText) * IIf(Me.ascending, 1, -1)
    End Function

有一个简单的acheive我的排序与字符串和整数一个ListView结果的方式?

Is there a simpler way to acheive my result of sorting a ListView with Strings and Integers?

注:

我省略张贴的ListView排序$ C这里$ C 的,因为它会一直聚集后颇有几分。我提供这充分说明它的链接。

I omitted posting the ListView Sort code here because it would've clustered the post quite a bit. I provided the link which explains it thoroughly.

推荐答案

下面是另一种方式。在您的排序code盘,当你调用0的琴弦的CompareTo

Here's another way. In your Sort code pad the strings with 0's when you call CompareTo.

If Integer.TryParse(xText, VBNull) Then
      xText = xText.PadLeft(6, "0"c)
End If
If Integer.TryParse(yText, VBNull) Then
      yText = yText.PadLeft(6, "0"c)
End If
Return xText.CompareTo(yText) * IIf(Me.ascending, 1, -1)

这条道路11004将被比较,011004和之前的110030出现,但填充仅用于比较,值将仍然显示为11004。

This way "11004" will be compared as "011004" and appear before "110030", but the padding is only for comparison, the value will still be displayed as "11004".

更新:

普通字符串将不填充现在进行排序。

Normal strings will be sorted without padding now.

这篇关于如何排序列表视图的列含有字符串的&安培;整型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆