Solved

Reorder a List Of String

Posted on 2012-03-20
16
414 Views
Last Modified: 2012-03-21
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.. ?
0
Comment
Question by:PhilippeRenaud
  • 9
  • 7
16 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37744847
You'd utilize the following logic
        Dim xList As New List(Of String)
        Dim ds = xList.Item(9)
        xList.Remove(ds)
        xList.Insert(2, ds)

Open in new window

I hope you follow. Remove item from index 9 and insert it into index 2
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747011
so i would do a For loop if I have many?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747185
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
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 17

Expert Comment

by:nepaluz
ID: 37747204
Could you just give me the entire list that you are working with?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747260
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
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37747439
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

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747516
it doesnt like eList.ToArray()

maybe because eList if not declared? what should it be? IEnumarable maybe?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747536
Public member 'ToArray' on type 'WhereSelectListIterator(Of String,String)' not found.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37747559
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

Open in new window

I have tried to convert /evaluate the Enumerable ToArray()
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747614
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?
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 37747670
I have re-jiged your code to:
        Dim ResList As New List(Of String)
        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 s In eList
            qList.Remove(s)
        Next
        For Each x In qList
            If x.Contains("Description") Then
                ResList.Add(x)
                Array.ForEach(eList, Sub(q) ResList.Add(q))
            Else
                ResList.Add(x)
            End If
        Next

Open in new window

And I get
res
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747754
what are your imports?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37747780
Imports System.Data.Linq
may be the one you are looking for ....what framework are you working with?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37747822
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..
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37747849
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.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37749632
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding bootstrap to only Content page and NOT master page 1 49
Generate Numbers in JQuery file 11 66
Syntax Issue with SSIS module 26 103
SSIS On fail action 5 38
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question