compare path strings

P

Peter Proost

Hi group, I want to compare path strings in order to sort them, assuming I
have got:

"a.txt"
"dir1\c.txt"
"e.txt"
"dir1\d.txt"

When I compare them using > "e.text" would be greater than "dir1\c.txt"
But I would like them to be sorted as followed
"a.txt"
"e.txt"
"dir1\c.txt"
"dir1\d.txt"

so I wrote the following function to compare the strings, but I was
wondering if I'm maybe missing some built in dot net function because for
the moment my function is lacking support for strings containing more than
one \

Private Function MyCompare(ByVal str1 As String, ByVal str2 As String) As
Boolean
str1 = LCase(str1)
str2 = LCase(str2)
If str1.IndexOf("\"c) <> -1 And str2.IndexOf("\"c) = -1 Then
Return True
Else
If str1.IndexOf("\"c) <> -1 And str2.IndexOf("\"c) <> -1 Then
If Mid(str1, 1, str1.IndexOf("\"c)) = Mid(str2, 1,
str2.IndexOf("\"c)) Then
Return Mid(str1, str1.IndexOf("\"c) + 2, str1.Length -
str1.IndexOf("\"c) + 2) > Mid(str2, str2.IndexOf("\"c) _ + 2, str2.Length -
str2.IndexOf("\"c) + 2)
Else
Return Mid(str1, 1, str1.IndexOf("\"c)) > Mid(str2, 1,
str2.IndexOf("\"c))
End If
Else
If str1.IndexOf("\"c) = -1 And str2.IndexOf("\"c) <> -1 Then
Return False
Else
If str1.IndexOf("\"c) = -1 And str2.IndexOf("\"c) = -1
Then
Return str1 > str2
End If
End If
End If
End If


End Function

Greetz,

Peter
 
R

Robert Schneider

Peter Proost said:
Hi group, I want to compare path strings in order to sort them, assuming I
have got:

"a.txt"
"dir1\c.txt"
"e.txt"
"dir1\d.txt"

When I compare them using > "e.text" would be greater than "dir1\c.txt"
But I would like them to be sorted as followed
"a.txt"
"e.txt"
"dir1\c.txt"
"dir1\d.txt"

so I wrote the following function to compare the strings, but I was
wondering if I'm maybe missing some built in dot net function because for
the moment my function is lacking support for strings containing more than
one \

Private Function MyCompare(ByVal str1 As String, ByVal str2 As String) As
Boolean
str1 = LCase(str1)
str2 = LCase(str2)
If str1.IndexOf("\"c) <> -1 And str2.IndexOf("\"c) = -1 Then
Return True
Else
If str1.IndexOf("\"c) <> -1 And str2.IndexOf("\"c) <> -1 Then
If Mid(str1, 1, str1.IndexOf("\"c)) = Mid(str2, 1,
str2.IndexOf("\"c)) Then
Return Mid(str1, str1.IndexOf("\"c) + 2, str1.Length -
str1.IndexOf("\"c) + 2) > Mid(str2, str2.IndexOf("\"c) _ + 2,
str2.Length -
str2.IndexOf("\"c) + 2)
Else
Return Mid(str1, 1, str1.IndexOf("\"c)) > Mid(str2, 1,
str2.IndexOf("\"c))
End If
Else
If str1.IndexOf("\"c) = -1 And str2.IndexOf("\"c) <> -1
Then
Return False
Else
If str1.IndexOf("\"c) = -1 And str2.IndexOf("\"c) = -1
Then
Return str1 > str2
End If
End If
End If
End If


End Function

Greetz,

Peter




You may want to try this:

Public Class Comparer
Implements IComparer(Of String)

Public Function Compare(ByVal x As String, ByVal y As String) As Integer
Implements System.Collections.Generic.IComparer(Of String).Compare
Dim numOfPSS_x As Integer = GetNumberOfPathSeparatorSigns(x)
Dim numOfPSS_y As Integer = GetNumberOfPathSeparatorSigns(y)
If numOfPSS_x = numOfPSS_y Then
Return x.CompareTo(y)
Else
If numOfPSS_x > numOfPSS_y Then
Return 1
Else
Return -1
End If
End If
End Function

Private Shared Function GetNumberOfPathSeparatorSigns(ByVal x As String)
As Integer
Dim count As Integer = 0
For i As Integer = 0 To x.Length - 1
If x.Chars(i) = "\"c Then
count += 1
End If
Next
Return count
End Function
End Class



Private Sub Test()
Dim stringList As New List(Of String)

stringList.Add("e.txt")
stringList.Add("dir1\c.txt")
stringList.Add("dir1\d.txt")
stringList.Add("a.txt")

stringList.Sort(New Comparer())

For Each s As String In stringList
Debug.Print(s)
Next
End Sub

Hope this is correct.

Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top