Help with sorting data in string variable in xml file using VB.NET

vcharles
vcharles used Ask the Experts™
on
Hi,

How do you sort data separated by ";" in a dataelement? For example my xml file contains a CTRY data element with the following data <CTRY>BEL,FRA,CAN,USA,DEU</CTRY>. How do I loop through all the records and sort the data alphabetically? For example <CTRY>BEL,CAN,FRA,DEU,USA</CTRY>.

Thanks,

Victor
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
string s = "BEL,FRA,CAN,USA,DEU";
            List<String> ls1 = s.Split(',').ToList();
            ls1.Sort();
ls1 is a list, sorted alphabetically, with the individual country abbreviations

Author

Commented:
Thanks for the code, do you have the solution in VB.NET?
Freelance programmer / Consultant
Commented:
After a little look into the help files (for the syntax for the split function):

        Dim s As String = "BEL,FRA,CAN,USA,DEU"
        Dim ls1 As List(Of String) = s.Split(New [Char]() {","c}).ToList
        ls1.Sort()
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Hi,

How would you load the xml file below and sort all the ctry data element?

<Root>
<Table1>
<Item>1</Item>
<Ctry>BEL,USA,CAN<Ctry>
</Table1>
<Table1>
<Item>2</Item>
<Ctry>BEL,USA,CAN<Ctry>
</Table1>
<Table1>
<Item>3</Item>
<Ctry>FRA,USA,CAN<Ctry>
</Table1>
<Table1>
<Item>4</Item>
<Ctry>USA,CAN,FRA,NLD<Ctry>
</Table1>
</Root>

Thanks,

V.
AndyAinscowFreelance programmer / Consultant

Commented:
I'd close this question and ask a new question with that.  My comment addresses (and solves) how to sort the elements in the comma separated string you gave.

ps.  I'd also have another think about what is required - it isn't obvious what you want from the XML.

Author

Commented:
Thank You.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial