开发者

sorting and getting uniques

i have a string that looks like this

"apples,fish,oranges,bananas,fish"

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


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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜