By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

compare path strings

P: n/a
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)
Jun 22 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Peter Proost" <pp*****@nospam.hotmail.comschrieb im Newsbeitrag
news:eI**************@TK2MSFTNGP06.phx.gbl...
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)


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

--
e-mail: r_.s_chnei_der\wein_gart_ner.com (remove all '_' and replace '\'
with
'@')

Jun 22 '07 #2

P: n/a
Thanks that seems to do the job

thanks again

Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Robert Schneider" <lo*******@the.postingschreef in bericht
news:ey**************@TK2MSFTNGP06.phx.gbl...
"Peter Proost" <pp*****@nospam.hotmail.comschrieb im Newsbeitrag
news:eI**************@TK2MSFTNGP06.phx.gbl...
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to
produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)


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

--
e-mail: r_.s_chnei_der\wein_gart_ner.com (remove all '_' and replace '\'
with
'@')

Jun 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.