• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

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
0
vcharles
Asked:
vcharles
1 Solution
 
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
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.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now