Reorder a list of String into a new list - vb.net

I want to reorder a list of strings into a different list of strings. I am creating multiple loops to do this, however i was wondering if there was a better way to do this.
Dim values As New List(Of String)
For Each val As String In vals
If val.Contains("10") And val.Contains("Year 1") Then
values.Add(val)
End If
Next
For Each val As String In vals
If val.Contains("20") And val.Contains("Year 1") Then
values.Add(val)
End If
Next
There are going to be quite a lot of these loops to do what I want can anyone offer any help or a better way to go about doing this? Note that I want the values list to return 10,10,10,20,20,20 so that lines that have 10 should be added first and lines with 20 then afterwards.
thanks,
Stefan

If you want it to be ordered by the year, this will work:
Dim result = (From item In values
Where item.ToLower Like "*year #*"
Let year = Regex.Match(item.Substring(item.ToLower.IndexOf("year ")), "\d+").Value
Order By year).ToList()
But you've said that you want it to be ordered by 10,20,30 and so on. But i don't see how this should work.
The value could be 8,25,4711 instead and how do want to find it at all in the String?
Contains only checks whether the line contains this substring or not, but you might have this number also in the year part(f.e. year 10) or elsewhere.
So your requirement looks very error-prone and vague.
Edit: If the text is so strict as in your comment's samples, you could simplify it to this, splitting each line into tokens separated by comma:
Dim result = (From item In values
Let Tokens = item.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Let Count = Int32.Parse(Tokens(3))
Let Year = Int32.Parse(Regex.Match(Tokens(5), "\d+").Value)
Order By Count, Year
Select item).ToList()

Dim values As List(Of String) = vals.OrderBy(Function(s)
If s.Contains("Year 1") Then
If s.Contains("10") Then Return 0
If s.Contains("20") Then Return 1
End If
Return Integer.MaxValue
End Function).ToList()
It sounds like you may also want to create a regular expression for this string, to pull out the fields you care about. That should make this perform a lot better. To help you with the correct expression we'll need to know a lot more about what the strings look like.

Related

Refined list sorting by substring integer after alphabetical sorting

I have some information in a list (called listLines). Each line below is in a List(Of String).
1|This is just a header
3|This is just a footer
2|3456789|0000000|12312312313|BLUE|1|35.00
2|7891230|0000000|45645645655|BLUE|1|22.00
2|7891230|0000000|45645645658|RED|2|13.00
2|3456789|0000000|12312312316|RED|2|45.00
2|3456789|0000000|12312312317|YELLOW|5|-9.00
2|3456789|0000000|12312312315|ORANGE|3|15.00
2|7891230|0000000|45645645659|YELLOW|5|32.00
2|3456789|0000000|12312312314|GREEN|4|-20.00
2|7891230|0000000|45645645656|GREEN|4|39.00
2|7891230|0000000|45645645657|ORANGE|3|-18.50
I'm doing a listLines.sort() on the list to sort it alphabetically. Below is what I get after the .sort().
1|This is just a header
2|3456789|0000000|12312312313|BLUE|1|35.00
2|3456789|0000000|12312312314|GREEN|4|-20.00
2|3456789|0000000|12312312315|ORANGE|3|15.00
2|3456789|0000000|12312312316|RED|2|45.00
2|3456789|0000000|12312312317|YELLOW|5|-9.00
2|7891230|0000000|45645645655|BLUE|1|22.00
2|7891230|0000000|45645645656|GREEN|4|39.00
2|7891230|0000000|45645645657|ORANGE|3|-18.50
2|7891230|0000000|45645645658|RED|2|13.00
2|7891230|0000000|45645645659|YELLOW|5|32.00
3|This is just a footer
With that said, I need to output this information to a file. I'm able to do this ok. I still have a problem though. There is a sequence number in the above data at position 5 just after the listed colors (RED, BLUE, ETC..) that you can see. It's just before the last value which is a decimal type.
I need to further sort this list, keeping it in alphabetical order since position 2 is an account number and I want to keep the account numbers grouped together. I just want them to be resorted in sequential order based on the sequence number.
I was looking at another thread trying to figure out how I can do this. I found a piece of code like listLines.OrderBy(Function(q) q.Substring(35)).ToArray. I think this would probably help me if this was a fixed length file, it isn't however. I was thinking I can do some kind of .split() to get the 5th piece of information and sort it but then it's going to unalphabetize and mix the lines back up because I don't know how to specify to still keep it alphabetical.
Right now I'm outputting my alphabetical list like below so I can format it with commas and double quotes.
For Each listLine As String In listLines
strPosition = Split(listLine, "|")
Dim i As Integer = 1
Dim iBound As Integer = UBound(strPosition)
Do While (i <= iBound)
strOutputText = strOutputText & Chr(34) & strPosition(i) & Chr(34) & ","
i += 1
Loop
My main question is how do I re-sort after .sort() to then get each account (position1) in sequential order (position 5)? OR EVEN BETTER, how can I do both at the same time?
The List(Of T) class has an overload of the Sort method that takes a Comparison(Of T) delegate. I would suggest that you use that. It allows you to write a method or lambda expression that will take two items and compare them any way you want. In this case, you could do that like this:
Dim items = New List(Of String) From {"1|This Is just a header",
"3|This Is just a footer",
"2|3456789|0000000|12312312313|BLUE|1|35.00",
"2|7891230|0000000|45645645655|BLUE|1|22.00",
"2|7891230|0000000|45645645658|RED|2|13.00",
"2|3456789|0000000|12312312316|RED|2|45.00",
"2|3456789|0000000|12312312317|YELLOW|5|-9.00",
"2|3456789|0000000|12312312315|ORANGE|3|15.00",
"2|7891230|0000000|45645645659|YELLOW|5|32.00",
"2|3456789|0000000|12312312314|GREEN|4|-20.00",
"2|7891230|0000000|45645645656|GREEN|4|39.00",
"2|7891230|0000000|45645645657|ORANGE|3|-18.50"}
items.Sort(Function(x, y)
Dim xParts = x.Split("|"c)
Dim yParts = y.Split("|"c)
'Compare by the first column first.
Dim result = xParts(0).CompareTo(yParts(0))
If result = 0 Then
'Compare by the second column next.
result = xParts(1).CompareTo(yParts(1))
End If
If result = 0 Then
'Compare by the sixth column last.
result = xParts(5).CompareTo(yParts(5))
End If
Return result
End Function)
For Each item In items
Console.WriteLine(item)
Next
If you prefer a named method then do this:
Private Function CompareItems(x As String, y As String) As Integer
Dim xParts = x.Split("|"c)
Dim yParts = y.Split("|"c)
'Compare by the first column first.
Dim result = xParts(0).CompareTo(yParts(0))
If result = 0 Then
'Compare by the second column next.
result = xParts(1).CompareTo(yParts(1))
End If
If result = 0 Then
'Compare by the sixth column last.
result = xParts(5).CompareTo(yParts(5))
End If
Return result
End Function
and this:
items.Sort(AddressOf CompareItems)
Just note that this is rather inefficient because it splits both items on each comparison. That's not a big deal for a small list but, if there were a lot of items, it would be better to split each item once and then sort based on those results.

Can I use a method that returns a list of strings in SSRS report code as the headers in a tablix?

I have table that needs to contain 50 columns for each half hour in the day (+2 for daylight savings). So each column will be HH1, HH2, HH3... HH50.
I have written this piece of code in the report properties code section.
Function GetHH() As List(Of String)
Dim headers As List(Of String) = new List(Of String)
For index As Integer = 1 to 50
headers.Add("HH" & index)
Next
return headers
End Function
Is there a way to use the output of this function as the headers of my tablix? Or will I need to add the headers to some sort of dataset in the database and add it from there?
The column group functionality would be well suited for this. As you mentioned, you would need to write a SQL statement to return these values in a dataset. Then you can set your column group to group on these values. This way your table always gets the right number of columns and you don't have to add them manually.

Populate list box from string on different lines

I have a query that returns a list of items that are in one listbox but not another. I want to put the results of that into a different listbox, problem is that when I try to put the results into a listbox, it puts them all on the same line. Say it returns three results (project_green Project_blue_ project_red), its adding them to one line in the list box as (project_greenproject_blueproject_red). I want each item to be on its own line but I can't get it there.
Here is the code I have
Dim result As List(Of String) = (From s1 As String In Me.ListBox1.Items Where Not Me.ListBox4.Items.Contains(s1) Select s1).ToList()
ListBox5.Items.Add(String.Join(Environment.NewLine, result))
Any help would be appreciated.
Thanks
Dim result = From s1 As String In ListBox1.Items Where Not ListBox4.Items.Contains(s1) Select s1
ListBox5.Items.AddRange(result)
This also saves you a bunch of extra memory. There's no need to allocate a List when the IEnumerable result form the linq query will already do on its own.
Though personally, I'm not a fan of the query comprehension syntax and prefer to write it this way:
Dim result = ListBox1.Items.Where(Function(s1) Not ListBox4.Items.Contains(s1))
ListBox5.Items.AddRange(result)

Count lines before specified string of Text File? In VB

is there a way to count the amount of lines before a specific line / string in a text file.
For Example:
1
2
3
4
5
6
7
8
9
Say i want to count the amount of line before '8'...
How would i do that?
thanks!
Hope that this actually you are looking for,
it will read all lines from a file specified. then find the IndexOf particular line(searchText) then add 1 to it will gives you the required count since index is0based.
Dim lines = File.ReadAllLines("f:\sample.txt")
Dim searchText As String = "8"
msgbox(Array.IndexOf(lines, searchText) + 1)
Here's another example using List.FindIndex(), which allows you to pass in a Predicate(T) to define how to make a match:
Dim fileName As String = "C:\Users\mikes\Documents\SomeFile.txt"
Dim lines As New List(Of String)(File.ReadAllLines(fileName))
Dim index As Integer = lines.FindIndex(Function(x) x.Equals("8"))
MessageBox.Show(index)
In the example above, we're looking for an exact match with "8", but you can make the predicate match whatever you like for more complex scenarios. Just make the function (the predicate) return True for what you want to be a match.
For example, a line containing "magic":
Function(x) x.ToLower().Contains("magic")
or a line that begins with a "FirstStep":
Function(x) x.StartsWith("FirstStep")
The predicate doesn't have to be a simple string function, it can be as complex as you like. Here's one that will find a string that ends with "UnicornFarts", but only on Wednesday and if Notepad is currently open:
Function(x) DateTime.Today.DayOfWeek = DayOfWeek.Wednesday AndAlso Process.GetProcessesByName("notepad").Length > 0 AndAlso x.EndsWith("UnicornFarts")
You get the idea...
Using a List, instead of an Array, is good for situations when you need to delete and/or insert lines into the contents before writing them back out to the file.

Code optimisation removing duplicates in VB

I'm looking to optimise my code. Specifically this process
Calculate a group of locations (basically squares on a grid)
Have a list of all the locations that have been calculated
Then I go through all these locations, 1 at a time.
The issue I'm having is removing or not including duplicate locations in the list. I've tried having a list of integers (integers to represent the location) but it's still very slow. To give you an idea of the numbers: I'm talking at least 15,000 different location calculations and around 1,000,000 possible locations.
Any help on this would be much appreciated!
Here is how I remove duplicates from an string array, perhaps it will be of help to you:
Dim OneDimensionalTable(1000) As String
....
OneDimensionalTable = RemoveDuplicates(OneDimensionalTable)
.....
Private Function RemoveDuplicates(ByVal items As String()) As String()
Dim noDupsArrList As New ArrayList()
For i As Integer = 0 To items.Length - 1
If Not noDupsArrList.Contains(items(i).Trim()) Then
noDupsArrList.Add(items(i).Trim())
End If
Next
Dim uniqueItems As String() = New String(noDupsArrList.Count - 1) {}
noDupsArrList.CopyTo(uniqueItems)
Return uniqueItems
End Function