Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why can I not sort the following?

Status
Not open for further replies.

iannuzzelli

Programmer
Jan 4, 2003
36
US
The Following example list is supposed to be sorted using a List View control.

Thinking it was my code I have tried sorting the list in Excel and the sorting results are the same.

Why does the '-' not line up in the sorting sequence?
Is there any way to get around this?

Sorted in Ascending order:
PS-05
PS-10-24
PS2-61
PS2-61P
PS-49
PS-52
PS-55

Sorted in Descending order:
PS-55
PS-52
PS-49
PS2-61P
PS2-61
PS-10-24
PS-05

 
This doesn't work, because by default the .NET Framework performs a "Word" Sort rather than a "String" Sort. A Word Sort excludes hyphens(-) and single quotes(') from sorts, unless there are two words that would be identical without the hyphen or single quote. If this occurs then the item without the hyphen or single quote would come before the second item.

Unfortunately, the .Net Framework does not provide us with a easily selected option to specify a String Sort. However, we can define a class that implements the IComparer Interface that uses this Sort Method. The following Class can be used to sort a list in a Case Insensitive Manner using the String Sort.
Code:
Public Class CaseInsensitiveUseHyphensComparer
    Implements IComparer

    Private myComp As CompareInfo
    Private myOptions As CompareOptions = CompareOptions.None

    ' Constructs a comparer using a specific set of options
    Public Sub New()
        Me.myComp = CultureInfo.CurrentCulture.CompareInfo

        ' StringSort = Uses ASCII Based Sorting Method (Hyphen in Order)
        Me.myOptions = CompareOptions.StringSort Or CompareOptions.IgnoreCase
    End Sub

    ' Compares strings with the CompareOptions specified in the constructor.
    Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
        If a = b Then
            Return 0
        End If
        If a Is Nothing Then
            Return -1
        End If
        If b Is Nothing Then
            Return 1
        End If

        Dim sa As String = a
        Dim sb As String = b
        If Not (sa Is Nothing) And Not (sb Is Nothing) Then
            Return myComp.Compare(sa, sb, myOptions)
        End If
        Throw New ArgumentException("a and b should be strings.")

    End Function
End Class
To use this new class just pass it as an argument to the Array.Sort() method. An Example Follows:
Code:
Dim vals() As String = {"PS-05", "PS-10-24", "PS2-61", _
                                "PS2-61P", "PS-49", "PS-52", _
                                "PS-55"}
        Dim s As String

        Array.Sort(vals, New CaseInsensitiveUseHyphensComparer())
The class contained in this example was adapted from a more generic class found at
 
Sorry, you will need to import the Sytem.Globalization Namespace to make this work. I've only been working with .NET for 3 weeks now, so there may be other mistakes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top