473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

compare path strings

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
2 2084
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
2
by: J.W. | last post by:
How do I compare strings in javascript? The "==" double equals or "!=" doesn't seem to work in this case. I'm sure string comparison has been explained before but searching Google didn't find...
3
by: Drew | last post by:
Hello - I am a converted VB programmer. What I am trying to do it compare two strings in an if statement. The problem is that when I use string.compare it always returns a negative 1. I have...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
14
by: Samuel R. Neff | last post by:
Why would you cast two strings to objects to compare them? I saw code in an MS sample on MSDN and don't get it. if ( (object)name == (object)attr.name ) { both "name" and "attr.name" are...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
3
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.