vb.net排序列表排序顺序不正确

时间:2015-11-06 13:56:01

标签: vb.net sortedlist

我正在尝试使用排序列表,但似乎没有按照我希望它排序的方式排序。我有一个包含字符串值的列表。

true

将这些值添加到我的排序列表中时,它将返回为:

<table class="table" id="exampleTable">
    <thead>
        <tr>
            <th>Firstname</th>
            <th>Sign Up Date</th>
        </tr>
    </thead>

    <tbody>

        <tr>
            <td>Peter</td>
            <td data-order="2015-11-13 12:00">13. November 2015</td>
        </tr>
        <tr>
            <td>Daniel</td>
            <td data-order="2015-08-06 13:44">06. August 2015</td>
        </tr>
        <tr>
            <td>Michael</td>
            <td data-order="2015-10-14 16:12">14. October 2015</td>
        </tr>
    </tbody>
</table>


<script>
    $(document).ready(function() {
        $('#exampleTable').DataTable();
    });
</script>

我知道这是因为我对字符串数字进行了排序,但是你怎么能避免这种情况?

为什么这些值以字符串形式输入?子编号有可能存在 像1.1; 1.2; 1.3 ..所以字符串类型必须保留,我不能转换成双打因为1,1&amp; 1,10会给我一个错误是相同的值。

如何对10之后而不是1之后的值进行排序?

添加:将项目添加到列表中的代码

2
4
7
1
3
5
6
8
9
10

3 个答案:

答案 0 :(得分:1)

您需要编写IComparer(Of T)的自定义实现来处理您想要的排序字符串。然后,您可以告诉排序列表使用此比较器对列表中的项目进行排序。比较器基本上会教你的列表如何确定一个项目是高于还是低于列表中的另一个项目。您可以使用它来创建您需要的任何类型的疯狂排序行为。

  • 您可以找到文档和示例here
  • 有一个 在其中一个答案中实现自然排序为IComparerthis问题。

答案 1 :(得分:0)

我使用以下扩展方法对列表进行字母数字排序(从C#快速转换):

private string _cn;
public string cn
{
    get { return _cn; }
    set
    {
        if (value != null)
            _cn = value;
    }
}

答案 2 :(得分:0)

我在互联网上找到了这个,感谢我能够像以前一样创建更好的搜索。谢谢大家!

Public Class AlphanumComparator     实现IComparer

Public Function Compare(ByVal x As Object,
            ByVal y As Object) As Integer Implements IComparer.Compare

    ' [1] Validate the arguments.
    Dim s1 As String = x
    If s1 = Nothing Then
        Return 0
    End If

    Dim s2 As String = y
    If s2 = Nothing Then
        Return 0
    End If

    Dim len1 As Integer = s1.Length
    Dim len2 As Integer = s2.Length
    Dim marker1 As Integer = 0
    Dim marker2 As Integer = 0

    ' [2] Loop over both Strings.
    While marker1 < len1 And marker2 < len2

        ' [3] Get Chars.
        Dim ch1 As Char = s1(marker1)
        Dim ch2 As Char = s2(marker2)

        Dim space1(len1) As Char
        Dim loc1 As Integer = 0
        Dim space2(len2) As Char
        Dim loc2 As Integer = 0

        ' [4] Collect digits for String one.
        Do
            space1(loc1) = ch1
            loc1 += 1
            marker1 += 1

            If marker1 < len1 Then
                ch1 = s1(marker1)
            Else
                Exit Do
            End If
        Loop While Char.IsDigit(ch1) = Char.IsDigit(space1(0))

        ' [5] Collect digits for String two.
        Do
            space2(loc2) = ch2
            loc2 += 1
            marker2 += 1

            If marker2 < len2 Then
                ch2 = s2(marker2)
            Else
                Exit Do
            End If
        Loop While Char.IsDigit(ch2) = Char.IsDigit(space2(0))

        ' [6] Convert to Strings.
        Dim str1 = New String(space1)
        Dim str2 = New String(space2)

        ' [7] Parse Strings into Integers.
        Dim result As Integer
        If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
            Dim thisNumericChunk = Integer.Parse(str1)
            Dim thatNumericChunk = Integer.Parse(str2)
            result = thisNumericChunk.CompareTo(thatNumericChunk)
        Else
            result = str1.CompareTo(str2)
        End If

        ' [8] Return result if not equal.
        If Not result = 0 Then
            Return result
        End If
    End While

    ' [9] Compare lengths.
    Return len1 - len2
End Function
相关问题