tags:

views:

45

answers:

3

i have a string that looks like this

"apples,fish,oranges,bananas,fish"

i want to be able to sort this list and get only the uniques. how do i do it in vb.net? please provide code

+1  A: 

I'm not a VB.NET programmer, but I can give you a suggestion:

  1. Split the string into an array
  2. Create a second array
  3. Cycle through the first array, adding any value that is not in the second.

Upon completion, your second array will have only unique values.

Jonathan Sampson
And you can use VB.Net's Array.Sort to sort the final array; and use the Join(array, delimiter) method to convert back to a string, if necessary.
Ed Schembor
+1 Thank you for the additional information, Ed.
Jonathan Sampson
+3  A: 

You can do it using LINQ, like this:

Dim input = "apples,fish,oranges,bananas,fish"
Dim strings = input.Split(","c).Distinct().OrderBy(Function(s) s)
SLaks
+6  A: 

A lot of your questions are quite basic, so rather than providing the code I'm going to provide the thought process and let you learn from implementing it.

Firstly, you have a string that contains multiple items separated by commas, so you're going to need to split the string at the commas to get a list. You can use String.Split for that.

You can then use some of the extension methods for IEnumerable<T> to filter and order the list. The ones to look at are Enumerable.Distinct and Enumerable.OrderBy. You can either write these as normal methods, or use Linq syntax.

If you need to get it back into a comma-separated string, then you'll need to re-join the strings using the String.Join method. Note that this needs an array so Enumerable.ToArray will be useful in conjunction.

Greg Beech
+1 "Teach a man to fish..." :)
Jonathan Sampson