list, array or collection for storing and sorting numbers ?

  • Thread starter Thread starter Pascal
  • Start date Start date
Hello
What is the best practice to have few random numbers to be sorted in
ascending or descending way? array, list, collection ?

http://www.scalpa.infohttp://scalpa98.blogspot.com/

Anything IEnumerable will do. I tend to use an Array if I'm manually
creating the variable.

////////////////////
'// Typed in message, so watch for errors

Dim myNumbers As Integer = { 2, 5, 3, 2, 4, 7 }

'// Sort ascending, or pass in a custom sorter
myNumbers.Sort()

'// Use LINQ for cooler sorting
Dim sorted = from number in myNumbers _
order by number Desc _
select number
////////////////////

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Aargh ! I watched the code (the clue is not enough for a beginner like me)
I have not moved an inch!
So I explain the goal...

I wonder if you could help me : i am trying to build a software for my
pupils (primary school) in which I will use random generated numbers
(integers or decimals) For example i need ten numbers appearing in ten
different labels, and by dragging and dropping them in 10 other labels, they
have to sort them in ascending or descending order. So when they click on a
button to verify their job, i need to verify if the numbers are from the set
generated by the computer (that's for avoiding the cheat:pupils could have
written ten simple numbers, from 1 to 11 by example, in the labels before
validating their job!)and if they are in the correct order.
The interface works well, drag and drop etc. too. But the generation of 10
random and unique numbers , poses a problem.
I tried this to generate unique or not numbers without success, what's wrong
here?
thanks for help
pascal
<pre>Public Shared Function GenArrayNbres(ByVal Lower As Long, ByVal Upper
As Long, _
Optional ByVal HowMany As Integer = 1, _
Optional ByVal Unique As Boolean = True) As Object

Dim c As New Collection
Dim list As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim ajout As Boolean = True
If HowMany &lt; ((Upper + 1) - (Lower - 1)) Then
Try
ReDim list(HowMany - 1) 'redimensinner le tableau en
fonction de la quantité de nombres à obtenir
list(0) = GenNombre(Lower, Upper) 'initialise le tableau

For i As Integer = 1 To HowMany - 1
If Unique Then ' si pas de doublon
'ReDim Preserve list(i) 'on redimensionne le tableau
list(i) = GenNombre(Lower, Upper) ' on tire un
nombre

Do 'on compare l'élément en cours avec chaque
élément du tableau
For j As Integer = 0 To i - 1 'list.Length - 2
If list(i) = list(j) Then 'l'élément existe
déjà
Unique = False
Exit For 'on sort de la boucle de test
End If
Next
list(i) = GenNombre(Lower, Upper) 'on tire à
nouveau
Loop Until Unique = True

Else
list(i) = GenNombre(Lower, Upper)
End If
Next

Catch ex As ApplicationException

End Try

Else
MsgBox("Attention aux valeurs choisies" &amp; Chr(13) &amp;
"max-min &gt; Nbres", MsgBoxStyle.Exclamation, "Erreur")
End If
GenArrayNbres = list
End Function</pre>
 
Pascal said:
Aargh ! I watched the code (the clue is not enough for a beginner like me)
I have not moved an inch!
So I explain the goal...

I wonder if you could help me : i am trying to build a software for my
pupils (primary school) in which I will use random generated numbers
(integers or decimals) For example i need ten numbers appearing in ten
different labels, and by dragging and dropping them in 10 other labels,
they have to sort them in ascending or descending order. So when they
click on a button to verify their job, i need to verify if the numbers are
from the set generated by the computer (that's for avoiding the
cheat:pupils could have written ten simple numbers, from 1 to 11 by
example, in the labels before validating their job!)and if they are in the
correct order.
The interface works well, drag and drop etc. too. But the generation of 10
random and unique numbers , poses a problem.
I tried this to generate unique or not numbers without success, what's
wrong here?

Not an answer to your question, but this should work:

Public Shared Function GenArrayNbres( _
ByVal Lower As Integer, ByVal Upper As Integer, _
Optional ByVal HowMany As Integer = 1, _
Optional ByVal Unique As Boolean = True) _
As List(Of Integer)

Dim rnd As New Random
Dim Result As New List(Of Integer)

Do
Dim Value = rnd.Next(Lower, Upper)
If Not Unique OrElse Not Result.Contains(Value) Then
Result.Add(Value)
End If
Loop Until Result.Count = HowMany

Return Result

End Function

Later, after presenting the values in an unsorted order, you can call the
Sort() method of the generic list and compare the values to those from the
pupils in a simple loop.


Armin
 
Whoah ! Fantastic that works very fine and it's fast, short .....elegant
etc...
I spent a lot of time to find a solution witout success and was reading msdn
article here http://msdn.microsoft.com/en-us/magazine/cc700332.aspx talking
about enumerable and link.... too difficult for me.

Thanks a lot for spending time and sharing your knowledge. I f you agree, i
will try to post a simple project on codeproject for other newbees like me !
pascal

--
http://www.scalpa.info
http://scalpa-production.blogspot.com/






































http://www.scalpa.info
http://scalpa98.blogspot.com/
 
Armin Zingler said:
Not an answer to your question, but this should work:

Public Shared Function GenArrayNbres( _
ByVal Lower As Integer, ByVal Upper As Integer, _
Optional ByVal HowMany As Integer = 1, _
Optional ByVal Unique As Boolean = True) _
As List(Of Integer)

Dim rnd As New Random
Dim Result As New List(Of Integer)

Do
Dim Value = rnd.Next(Lower, Upper)
If Not Unique OrElse Not Result.Contains(Value) Then
Result.Add(Value)
End If
Loop Until Result.Count = HowMany

Return Result

End Function

Later, after presenting the values in an unsorted order, you can call the
Sort() method of the generic list and compare the values to those from the
pupils in a simple loop.


Armin
Hi Armin,

Could you explain the following line in the code you posted above ...
If Not Unique OrElse Not Result.Contains(Value) Then
I don't understand "Unique". Even if you were posting pseudocode I am still
confused because
Not Result.Contains(Value) is the same thing as unique, isn't it?

Thanks, Bob
 
eBob.com said:
Could you explain the following line in the code you posted above ...
If Not Unique OrElse Not Result.Contains(Value) Then
I don't understand "Unique". Even if you were posting pseudocode I am
still confused because
Not Result.Contains(Value) is the same thing as unique, isn't it?

Unique is a parameter that I took from you own declaration. My
interpretation was that it should tell the function if each random number
has to be unique in the array. So, the condition means: If the values don't
have to be unique (Not Unique), or, otherwise (OrElse), if the value is not
in the list yet (Not Result.Contains), then add it to the list (Result.Add).


Armin
 
Armin Zingler said:
Unique is a parameter that I took from you own declaration. My
interpretation was that it should tell the function if each random number
has to be unique in the array. So, the condition means: If the values
don't have to be unique (Not Unique), or, otherwise (OrElse), if the value
is not in the list yet (Not Result.Contains), then add it to the list
(Result.Add).


Armin

Thanks Armin. I wasn't the OP and didn't look closely enough at his code.
While I am at it ... thanks for all your posts, in response to my questions
and those of others, from which I have learned so much.

Bob
 
Back
Top