Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: themindmaster en 13 Octubre 2013, 17:02 pm



Título: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 17:02 pm
hola a todos, necesito una manito con este programa XD este es el diseño:

(http://img7.imageshack.us/img7/9440/gv27.jpg)

mi codigo hasta ahora es este :( :
Código
  1. Private Sub cmdañadir_Click()
  2. lstnumero.AddItem (txtnumero)
  3.  
  4. End Sub
  5.  
  6. Private Sub cmdordenar_Click()
  7. If optascendente Then
  8.  
  9.  
  10. End If
  11.  
  12. End Sub
  13.  

la función que quiero que haga este programa es que cuando le des click al botom añadir, añada el numero que pongas en el primer text y cuando le des a ascendente o descendente los ordene de forma ascendente o descendente XD espero me entiende y gracias de antemano.

saludos cordiales!


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 19:09 pm
Ya te ideaste un algoritmo?


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 19:30 pm
no T-T


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 19:38 pm
ahí tienes.


http://es.wikipedia.org/wiki/Ordenamiento_de_burbuja


saludos


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 19:53 pm
esta un poco largo pero lo vere XD gracias :)


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 19:55 pm
la documentación es larga pero el algoritmo te quedara en menos de 15 lineas.

culaquier duda pregunta.

saludos


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Mad Antrax en 13 Octubre 2013, 19:57 pm
Es más sencillo si aplicas el ordenamiento por Inserción

http://es.wikipedia.org/wiki/Ordenamiento_por_inserci%C3%B3n

Código
  1. Private Sub insertionSort(ByVal numbers() As Integer) ' Es una función,
  2. 'debemos pasarle el array de números desde el Sub Main()
  3.  
  4.        Dim i, j, index As Integer
  5.        i = 1
  6.  
  7.        Do
  8.            index = numbers(i)
  9.            j = i - 1
  10.  
  11.            While ((j >= 0) And (numbers(j) > index))
  12.                numbers(j + 1) = numbers(j)
  13.                j = j - 1
  14.            End While
  15.            numbers(j + 1) = index
  16.            i = i + 1
  17.        Loop Until i > (UBound(v))
  18.    End Sub


Tambien tienes el ordenamiento por Selección

Código
  1. For i = 1 To n - 1
  2.   minimo = i
  3.   For j = i + 1 To n
  4.      If x(minimo) > x(j) Then
  5.         minimo = j
  6.      End If
  7.   Next j
  8.   temp = x(i)
  9.   x(i) = x(minimo)
  10.   x(minimo) = temp
  11. Next i


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 20:02 pm
Que se supone que es numbers si se podria saber?


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 20:15 pm
No me funciona :(


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 20:31 pm
Que no te funciona?


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 20:33 pm
T_____T otro programa, en el cual tengo que pedir ayuda :( perdonen las molestias enserio :( T_T  el codigo no me funciona  me da error al principio :(


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 20:38 pm
El insertionSort esta hecho en vb.net.

pero que has hecho que no te funciona?


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 20:50 pm
entonces el codigo de arriba no funciona?


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 20:54 pm
Solo quiero que me ordene los numeros añadidos al listbox si le doy al optionbotom ascendente me los ordenara de manera ascendente pero si le doy a descendente me los ordenara de manera descendente


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 21:05 pm
A ver.

Te la pongo facil.

usa este algoritmo

Código
  1. 'true ascendente, false descendente
  2. Public Sub BubbleSort(ByRef iaArray() As Long, Optional ByVal tipo As Boolean = True)
  3.  
  4. Dim n As Integer, i As Integer, j As Integer
  5.  
  6.    n = UBound(iaArray)
  7.  
  8.    For i = (n - 1) To 0 Step -1
  9.  
  10.        For j = 0 To i
  11.  
  12. If tipo = True Then
  13.            If iaArray(j) > iaArray(j + 1) Then
  14.            Swap iaArray(j), iaArray(j + 1)
  15.  
  16.            End If
  17. Else
  18. If iaArray(j) < iaArray(j + 1) Then
  19.            Swap iaArray(j), iaArray(j + 1)
  20.  
  21.            End If
  22.            End If
  23.  
  24.  
  25.        Next j
  26.  
  27.    Next i
  28.  
  29. End Sub


que tienes que hacer. lo siguiente.



coger todos los elementos del listbox en un array.


luego

limpiar el listbox

luego llamar la rutina

Call BubbleSort(arreglo(), True)

luego llenar el listbox otra vez.


saludos





Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 21:15 pm
me da error :( dice que  se esperaba endsub T___T
saludos!


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 21:20 pm
Muestra tu código completo  :rolleyes:


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 21:37 pm
jajajaja perdona se me olvido recargar la pagina :)  mira el codigo
Código
  1. Private Sub cmdañadir_Click()
  2. lstnumero.AddItem (txtnumero)
  3.  
  4. End Sub
  5.  
  6. Private Sub cmdordenar_Click()
  7. Public Sub BubbleSort(ByRef iaArray() As Long, Optional ByVal tipo As Boolean = True)
  8.  
  9. Dim n As Integer, i As Integer, j As Integer
  10.  
  11.    n = UBound(iaArray)
  12.  
  13.    For i = (n - 1) To 0 Step -1
  14.  
  15.        For j = 0 To i
  16.  
  17. If tipo = True Then
  18.            If iaArray(j) > iaArray(j + 1) Then
  19.            Swap iaArray(j), iaArray(j + 1)
  20.  
  21.            End If
  22. Else
  23. If iaArray(j) < iaArray(j + 1) Then
  24.            Swap iaArray(j), iaArray(j + 1)
  25.  
  26.            End If
  27.            End If
  28.  
  29.  
  30.        Next j
  31.  
  32.    Next i
  33.  
  34.  
  35.  
  36.  
  37.  
  38. End Sub


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 21:46 pm
Estas declarando un rutina dentro de otra :S

debería ser algo así.

Código
  1.  
  2.  
  3.  
  4. Private Sub Ordenar_Click()
  5. Dim i As Long
  6. Dim arreglo() As Long
  7. ReDim arreglo(List1.ListCount - 1)
  8. For i = 0 To List1.ListCount - 1
  9. arreglo(i) = Int(List1.List(i))
  10. Next
  11. For i = 0 To UBound(arreglo)
  12.  
  13. Next
  14. Call BubbleSort(arreglo(), True)
  15. List1.Clear
  16.  
  17. For i = 0 To UBound(arreglo)
  18. List1.AddItem arreglo(i)
  19. Next
  20.  
  21.  
  22.  
  23. End Sub
  24.  
  25.  
  26.  
  27.  
  28. 'true ascendente, false descendente
  29. Public Sub BubbleSort(ByRef iaArray() As Long, Optional ByVal tipo As Boolean = True)
  30.  
  31. Dim n As Integer, i As Integer, j As Integer
  32.  
  33.    n = UBound(iaArray)
  34.  
  35.    For i = (n - 1) To 0 Step -1
  36.  
  37.        For j = 0 To i
  38.  
  39. If tipo = True Then
  40.            If iaArray(j) > iaArray(j + 1) Then
  41.            Swap iaArray(j), iaArray(j + 1)
  42.  
  43.            End If
  44. Else
  45. If iaArray(j) < iaArray(j + 1) Then
  46.            Swap iaArray(j), iaArray(j + 1)
  47.  
  48.            End If
  49.            End If
  50.  
  51.  
  52.        Next j
  53.  
  54.    Next i
  55.  
  56. End Sub
  57.  
  58.  
  59.  
  60.  
  61.  
  62. Private Sub Swap(ByRef xArg1, ByRef xArg2)
  63.  
  64.    Dim xTmp
  65.  
  66.    xTmp = xArg2
  67.  
  68.    xArg2 = xArg1
  69.  
  70.    xArg1 = xTmp
  71.  
  72. End Sub
  73.  
  74.  
  75.  
  76.  
  77.  


PD: te recomiendo que leas mas sobre vb6( al menos lo básico ¬¬) lo estas haciendo al azar.

saludos


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: themindmaster en 13 Octubre 2013, 21:57 pm
este es el codigo que puse y me da un errorsillo:
Código
  1. Private Sub cmdañadir_Click()
  2. lstnumero.AddItem (txtnumero)
  3.  
  4. End Sub
  5.  
  6. Private Sub cmdordenar_Click()
  7. Dim i As Long
  8. Dim arreglo() As Long
  9. ReDim arreglo(List1.ListCount - 1)
  10. For i = 0 To List1.ListCount - 1
  11. arreglo(i) = Int(List1.List(i))
  12. Next
  13. For i = 0 To UBound(arreglo)
  14.  
  15. Next
  16. Call BubbleSort(arreglo(), True)
  17. List1.Clear
  18.  
  19. For i = 0 To UBound(arreglo)
  20. List1.AddItem arreglo(i)
  21. Next
  22.  
  23.  
  24.  
  25. End Sub
  26.  
  27.  
  28.  
  29.  
  30. 'true ascendente, false descendente
  31. Public Sub BubbleSort(ByRef iaArray() As Long, Optional ByVal tipo As Boolean = True)
  32.  
  33. Dim n As Integer, i As Integer, j As Integer
  34.  
  35.    n = UBound(iaArray)
  36.  
  37.    For i = (n - 1) To 0 Step -1
  38.  
  39.        For j = 0 To i
  40.  
  41. If tipo = True Then
  42.            If iaArray(j) > iaArray(j + 1) Then
  43.            Swap iaArray(j), iaArray(j + 1)
  44.  
  45.            End If
  46. Else
  47. If iaArray(j) < iaArray(j + 1) Then
  48.            Swap iaArray(j), iaArray(j + 1)
  49.  
  50.            End If
  51.            End If
  52.  
  53.  
  54.        Next j
  55.  
  56.    Next i
  57.  
  58. End Sub
  59.  
  60.  
  61.  
  62.  
  63.  
  64. Private Sub Swap(ByRef xArg1, ByRef xArg2)
  65.  
  66.    Dim xTmp
  67.  
  68.    xTmp = xArg2
  69.  
  70.    xArg2 = xArg1
  71.  
  72.    xArg1 = xTmp
  73.  
  74. End Sub
  75.  
error 424 se requiere un objeto :(


Título: Re: Ayuda ordenar un list de manera ascendente o descendente
Publicado por: Danyfirex en 13 Octubre 2013, 22:43 pm
Ya te dije tienes que leer mas.

el error es por el nombre de los controles que alguno no coincide. ademas falta terminarlo así no va a andar.