Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim Bytes As Byte() = {22, 123, 1, 1, 0, 22}


I want order and disticnt Bytes array.
Bytes = {0,1,22,123}


How can i do?
Thanks

What I have tried:

Dim Bytes As Byte() = {22, 123, 1, 1, 0, 22}
Dim ListByte As List(Of Byte) = Bytes.ToList
ListByte = ListByte.OrderBy(Function(x) x).ToList.Distinct
Posted
Updated 18-Apr-19 17:59pm

This can be done a little cleaner like this:
VB.NET
Dim bytes As Byte() = {22, 123, 1, 1, 0, 22}
bytes = bytes.Distinct().OrderBy(Function(x) x).ToArray()
Distinct can be called on any IEnumerable, which an array of Byte is. In your case, Distinct() will return an IEnumerable(Of Byte). This has the effect of making the next operation cost a little less since you're not sorting as many elements.

The call to OrderBy() can also be called on any IEnumerable, which the Distinct call just returned. It returns an IOrderedEnumerable(Of Byte), which is a subclass of IEnumerable. What that means is you can treat any IOrderedEnumerable the same as if it was an IEnumerable, which is what you're going to do next.

The last operation is simple. It converts the result of the OrderBy back to an array. That's easily done with the call to ToArray. It creates a new array out of any IEnumerable.

Done. YOu've got your sorted, distinct array.
 
Share this answer
 
v2
Comments
gacar 20-Apr-19 13:30pm    
Many thanks for solution and explain.
You need to tack "ToList" on the end; Distinct is returning some other type of collection.

(Use "var" until you work out the specifics).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900