Sorting Arrays

C

cmdolcet69

Im trying to compare arrayindex slots in a array. I have an arraylist
of size (n) and once all the arraylist slots are filled i would like
to compare each slot so that they are all unique number.



Example: (455,453,456,435,437,333) this a unique arraylist
(455,435,455,455,422,222) this has multiple values and
will not work.

However in the code below it will loop through each slot however how
can i write code that will compare each slot against the other. I dont
need to worry about the condition of multiple number between slot 1
and slot 2. My only worry is multiple number on the next seqnetial
slot....below is that code i have started.

Public Sub SortArraylist()

For Each intarrayindex In _ReadingArrayList3
If (intarrayindex <> intarrayindex) Then

Else
End If
Next
End Sub
 
A

Andrew Morton

cmdolcet69 said:
Im trying to compare arrayindex slots in a array. I have an arraylist
of size (n) and once all the arraylist slots are filled

*That's* the time to do the checking: when you're adding entries. If the
value already exists in the arraylist then don't add it.
i would like
to compare each slot so that they are all unique number.

Andrew
 
C

cmdolcet69

*That's* the time to do the checking: when you're adding entries. If the
value already exists in the arraylist then don't add it.


Andrew

How would i do that check what code could i place in the existing code
would do that check for me.
 
A

Andrew Morton

cmdolcet69 said:
How would i do that check what code could i place in the existing code
would do that check for me.

Completely untested - check the syntax in the help:

' only add the new element if it is unique
If Not(myArrayList.Contains(newElement)) then
myArrayList.Add(newElement)
End If

Andrew
 
P

Phill W.

cmdolcet69 said:
Im trying to compare arrayindex slots in a array. I have an arraylist
of size (n) and once all the arraylist slots are filled i would like
to compare each slot so that they are all unique number.

No. Check for uniqueness as you /add/ each item - if it's already
present, don't add another one.
I can't recall if the ArrayList has a Contains() method; if not, you may
have to use a Dictionary-type class, say a HashTable or, if you have
VB'2005, use a List(Of Integer):

Dim aList as ArrayList ' or List(Of Integer)

If ( Not aList.Contains( value ) ) Then
aList.Add( value )
End If

HTH,
Phill W.
 
A

Andrew Morton

Phill said:
No. Check for uniqueness as you /add/ each item - if it's already
present, don't add another one.
I can't recall if the ArrayList has a Contains() method; if not, you
may have to use a Dictionary-type class, say a HashTable

Just a comment: if the OP used a HashTable, there would be no duplication
issue because

myHashTable(value)=value

adds it if it isn't there already, and overwrites it if it is there.

Andrew
 
G

Guest

Like this


Dim Ht As New Hashtable()
Dim dummyArr() As Integer = {100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 100,
100, 3, 0, 4, 5, 5}
For Each val As Integer In dummyArr
Ht(val) = val
Next
'proove that all items are filtered

For Each val As Integer In Ht.Values
Debug.WriteLine(val)
Next


regards

Michel
 
B

Brian Gideon

Completely untested - check the syntax in the help:

' only add the new element if it is unique
If Not(myArrayList.Contains(newElement)) then
myArrayList.Add(newElement)
End If

Andrew

If there are a lot of items in the array list it might help to keep
them in a Dictionary as well to facilitate a quicker check.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top