Help with removing duplicates and sorting data from string variable using vb.net

Hi,

How do I remove  duplicates from a string variable and sort the values? for example is S = '55','44','11','2','1" how do i obtain S = '1','2','11','44','55'?

Thanks,

Victor
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
sorting is easy use 'Array.Sort(s)' but this does an alphabetic sort

Another way is to use lists
  Sub Main()
        Dim s As List(Of Integer) = New List(Of Integer)
        s.Add(55)
        s.Add(55)
        s.Add(44)
        s.Add(11)
        s.Add(2)
        s.Add(1)
        s.Add(1)
        s.Sort()
        For Each item As String In s
            Console.Write(item & " ")
        Next
        Console.WriteLine()
        Dim result As List(Of Integer) = s.Distinct().ToList
        For Each element As Integer In result
            Console.WriteLine(element)
        Next
        Console.ReadLine()

    End Sub

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
If this is a follow up on your previous question, then I would simply suggest that you do the job in the query that you use to retrieve the data from the database. Simply add a DISTINCT and an ORDER BY clause to your SQL command, and your DataTable will come out without the duplicates and already ordered, so the code you have to build your string will automatically present it  correctly.

You probably know the ORDER BY. If DISTINCT is new for you, you will find a good discussion with examples of its use at http://www.techonthenet.com/sql_server/distinct.php
0
vcharlesAuthor Commented:
Thank you for both solutions.
Victor
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

ArkCommented:
        Dim s = "'55','44','11','2','1','44','55'"
        Dim trimmed = s.TrimStart("'").TrimEnd("'")
        Dim arr = trimmed.Split(New String() {"','"},
                            StringSplitOptions.RemoveEmptyEntries).
                            Select(Function(x) CInt(x))
        Dim result = "'" & String.Join("','", arr.Distinct.OrderBy(Function(x) x).ToArray) & "'"
        MsgBox(result)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ArkCommented:
Oops, just read your previous question. You can use:
Dim S As String = "'" & String.Join ("','", _
(From id In dt.AsEnumerable() _
Select CInt(id.Field(Of String)("ID"))). _
Distinct().OrderBy(Function(x) x).ToArray) & "'"

Open in new window


PS. If your ID field is already integer type (not string) you can ommit CInt()
0
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.