Philippe Renaud
asked on
Reorder a List Of String
Hello EE,
I have a list of String in Vb.NET with some elements.
My problem is that all the strings that contains "Total" are always at the end and since I build an excel spreadsheet with that list my totals are at the end and they should be somewhere at the begening....
How could I re-order my List(Of String) so that elements that contains "Total" would need to be "Cut and paste' at let's say Index 3 ??
lets say there is 3 string with total and the first one is at index 10 .. well all those 9 would need to be at index 3,4 and 5 and all the other strings would need to modify their index so everything is fine in the list
is this possible.. ?
I have a list of String in Vb.NET with some elements.
My problem is that all the strings that contains "Total" are always at the end and since I build an excel spreadsheet with that list my totals are at the end and they should be somewhere at the begening....
How could I re-order my List(Of String) so that elements that contains "Total" would need to be "Cut and paste' at let's say Index 3 ??
lets say there is 3 string with total and the first one is at index 10 .. well all those 9 would need to be at index 3,4 and 5 and all the other strings would need to modify their index so everything is fine in the list
is this possible.. ?
ASKER
so i would do a For loop if I have many?
ASKER
I did this but I have logic error because It will only get the same item multiple times can you help fix it?
myCol is the list of string
Dim i As Integer = 2
For x As Integer = 0 To myCol.Count - 1
If myCol(x).Contains("Total") Then
Dim ds = myCol.Item(x)
myCol.Remove(x)
myCol.Insert(i, ds)
i += 1
End If
Next
myCol is the list of string
Dim i As Integer = 2
For x As Integer = 0 To myCol.Count - 1
If myCol(x).Contains("Total")
Dim ds = myCol.Item(x)
myCol.Remove(x)
myCol.Insert(i, ds)
i += 1
End If
Next
Could you just give me the entire list that you are working with?
ASKER
yes:
dgv_ID
dgv_Key
dgv_Description
dgv_01
dgv_02
dgv_B01
dgv_B02
dgv_Total01
dgv_Total02
dgv_TotalB01
dgv_TotalB02
I would need this result:
dgv_ID
dgv_Key
dgv_Description
dgv_Total01
dgv_Total02
dgv_TotalB01
dgv_TotalB02
dgv_01
dgv_02
dgv_B01
dgv_B02
dgv_ID
dgv_Key
dgv_Description
dgv_01
dgv_02
dgv_B01
dgv_B02
dgv_Total01
dgv_Total02
dgv_TotalB01
dgv_TotalB02
I would need this result:
dgv_ID
dgv_Key
dgv_Description
dgv_Total01
dgv_Total02
dgv_TotalB01
dgv_TotalB02
dgv_01
dgv_02
dgv_B01
dgv_B02
Here's what Ihave
Dim qList As New List(Of String) From {"dgv_ID", "dgv_Key", "dgv_Description", "dgv_01", "dgv_02", "dgv_B01", "dgv_B02", "dgv_Total01", "dgv_Total02", "dgv_TotalB01", "dgv_TotalB02"}
Dim eList = From x In qList Where x.Contains("Total") Select x
For Each x In qList
If x.Contains("Description") Then
Array.ForEach(eList.ToArray, Sub(q) qList.Remove(q))
Dim xIndex = qList.IndexOf(x) + 1
qList.InsertRange(xIndex, eList)
Exit For
End If
Next
ASKER
it doesnt like eList.ToArray()
maybe because eList if not declared? what should it be? IEnumarable maybe?
maybe because eList if not declared? what should it be? IEnumarable maybe?
ASKER
Public member 'ToArray' on type 'WhereSelectListIterator(O f String,String)' not found.
try
Dim qList As New List(Of String) From {"dgv_ID", "dgv_Key", "dgv_Description", "dgv_01", "dgv_02", "dgv_B01", "dgv_B02", "dgv_Total01", "dgv_Total02", "dgv_TotalB01", "dgv_TotalB02"}
Dim eList = (From x In qList Where x.Contains("Total") Select x).ToArray
For Each x In qList
If x.Contains("Description") Then
Array.ForEach(eList, Sub(q) qList.Remove(q))
Dim xIndex = qList.IndexOf(x) + 1
qList.InsertRange(xIndex, eList)
Exit For
End If
Next
I have tried to convert /evaluate the Enumerable ToArray()
ASKER
I have now:
NullReferenceException
Object reference not set to an instance of an object.
on line
Array.ForEach(eList, Sub(q) qList.Remove(q))
when mouseOver on eList on that line, I see that eList if filled with the total strings
maybe the error if the Sub(q) remove?
NullReferenceException
Object reference not set to an instance of an object.
on line
Array.ForEach(eList, Sub(q) qList.Remove(q))
when mouseOver on eList on that line, I see that eList if filled with the total strings
maybe the error if the Sub(q) remove?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what are your imports?
Imports System.Data.Linq
may be the one you are looking for ....what framework are you working with?
may be the one you are looking for ....what framework are you working with?
ASKER
mmm should be 4
i manage to get it to work by creating a new lsit of string and your eList i did at the end .ToList()
then myNewList = eList
and in your Array.ForEach I typed: myNewList.ToArray()
like this I have the same result as you otherwise it was the same error....very strange..
i manage to get it to work by creating a new lsit of string and your eList i did at the end .ToList()
then myNewList = eList
and in your Array.ForEach I typed: myNewList.ToArray()
like this I have the same result as you otherwise it was the same error....very strange..
Did you get it to work though? I changed the Lists to Array because of the errors you were getting earlier. now that you have fixed the Linq issue with the imports, you can work with list entirely, and you should be able to get the results without issue.
ASKER
i couldnt resolve even with the import. maybe like you said its the framework.
but its working with what I modified like I told you
thanks for the help Nepaluz
but its working with what I modified like I told you
thanks for the help Nepaluz
Open in new window
I hope you follow. Remove item from index 9 and insert it into index 2