Ver Mensaje Individual
  #10 (permalink)  
Antiguo 13/04/2015, 04:14
Fattony
 
Fecha de Ingreso: abril-2015
Mensajes: 6
Antigüedad: 9 años, 6 meses
Puntos: 0
Pregunta Atasco con codigo sorteo 2

Hola
Ya postee un anterior codigo y crei que lo habia solucionado pero ahora me ha surgido otro problema. Como utilice el sort para ordenar las puntuaciones de mayor a menor tuve que añadirle al array tambien los nombres de los jugadores por lo que no me ordena los numeros correctamente, y por ende los nombres tampoco.
Lo que es mas extraño es la forma que tiene de ordenarlos y que solo pase con ciertos numeros, uno en toda la lista quizas.

Os dejo aqui el codigo para que, si vosotros sabeis cómo, no tenga que cambiarlo mucho pero que me lo ordene siempre. Primero os dejo el HTML asi lo podeis probar:
Código HTML:
Ver original
  1. <!DOCTYPE HTML>
  2.     <meta charset="utf-8" />
  3.     <title></title>
  4.     <script type="text/javascript" src="sorteo.js">
  5.     </script>
  6. </head>
  7.     <h1>Sorteo</h1>
  8.     <p>Escribe aquí el nombre del jugador:
  9.     <input type="text" id="jugador1"/>
  10.     Escribe aquí la puntuacion obtenida:
  11.     <input type="text" id="puntuacion1"/>
  12.     <br/>
  13.     Escribe aquí el nombre del jugador:
  14.     <input type="text" id="jugador2"/>
  15.     Escribe aquí la puntuacion obtenida:
  16.     <input type="text" id="puntuacion2"/>
  17.     <br/>
  18.     Escribe aquí el nombre del jugador:
  19.     <input type="text" id="jugador3"/>
  20.     Escribe aquí la puntuacion obtenida:
  21.     <input type="text" id="puntuacion3"/>
  22.     <br/>
  23.     Escribe aquí el nombre del jugador:
  24.     <input type="text" id="jugador4"/>
  25.     Escribe aquí la puntuacion obtenida:
  26.     <input type="text" id="puntuacion4"/>
  27.     <br/>
  28.     Escribe aquí el nombre del jugador:
  29.     <input type="text" id="jugador5"/>
  30.     Escribe aquí la puntuacion obtenida:
  31.     <input type="text" id="puntuacion5"/>
  32.     <br/>
  33.     Escribe aquí el nombre del jugador:
  34.     <input type="text" id="jugador6"/>
  35.     Escribe aquí la puntuacion obtenida:
  36.     <input type="text" id="puntuacion6"/>
  37.     <br/>
  38.     Escribe aquí el nombre del jugador:
  39.     <input type="text" id="jugador7"/>
  40.     Escribe aquí la puntuacion obtenida:
  41.     <input type="text" id="puntuacion7"/>
  42.     <br/>
  43.     Escribe aquí el nombre del jugador:
  44.     <input type="text" id="jugador8"/>
  45.     Escribe aquí la puntuacion obtenida:
  46.     <input type="text" id="puntuacion8"/>
  47.     <br/>
  48.     Escribe aquí el nombre del jugador:
  49.     <input type="text" id="jugador9"/>
  50.     Escribe aquí la puntuacion obtenida:
  51.     <input type="text" id="puntuacion9"/>
  52.     <br/>
  53.     Escribe aquí el nombre del jugador:
  54.     <input type="text" id="jugador10"/>
  55.     Escribe aquí la puntuacion obtenida:
  56.     <input type="text" id="puntuacion10"/>
  57.     <br/>
  58.     Escribe aquí el nombre del jugador:
  59.     <input type="text" id="jugador11"/>
  60.     Escribe aquí la puntuacion obtenida:
  61.     <input type="text" id="puntuacion11"/>
  62.     <br/>
  63.     Escribe aquí el nombre del jugador:
  64.     <input type="text" id="jugador12"/>
  65.     Escribe aquí la puntuacion obtenida:
  66.     <input type="text" id="puntuacion12"/>
  67.     <br/>
  68.     Escribe aquí el nombre del jugador:
  69.     <input type="text" id="jugador13"/>
  70.     Escribe aquí la puntuacion obtenida:
  71.     <input type="text" id="puntuacion13"/>
  72.     <br/>
  73.     Escribe aquí el nombre del jugador:
  74.     <input type="text" id="jugador14"/>
  75.     Escribe aquí la puntuacion obtenida:
  76.     <input type="text" id="puntuacion14"/>
  77.     <br/>
  78.     Escribe aquí el nombre del jugador:
  79.     <input type="text" id="jugador15"/>
  80.     Escribe aquí la puntuacion obtenida:
  81.     <input type="text" id="puntuacion15"/>
  82.  
  83.     </p>
  84.     <h2>
  85.         <input type="button" id="boton" value="Aceptar"/>
  86.     </h2>
  87.     <script type="text/javascript">
  88.     inicio();
  89.     </script>
  90. </body>
  91. </html>
------------------------------------------------------------------------------------

Aqui os dejo el js que yo llamo sorteo.js:

Código Javascript:
Ver original
  1. var a, a1, b, b2, c, c3, d, d4, e, e5 , f, f6, g, g7, h, h8, i, i9, j, j10, k, k11, l, l12, m, m13, n, n14, o, o15;//aqui son los 30 cuadros de texto, 15 por nombres y 15 por puntuacion
  2. var z;//var para el boton y que dispare la funcion sortear()
  3. function inicio()
  4. {
  5.  
  6. a = document.getElementById("jugador1");
  7. a1 = document.getElementById("puntuacion1");
  8.  
  9.  
  10. b = document.getElementById("jugador2");
  11. b2 = document.getElementById("puntuacion2");
  12.  
  13.  
  14.  
  15. c = document.getElementById("jugador3");
  16. c3 = document.getElementById("puntuacion3");
  17.  
  18.  
  19.  
  20. d = document.getElementById("jugador4");
  21. d4 = document.getElementById("puntuacion4");
  22.  
  23. e = document.getElementById("jugador5");
  24. e5 = document.getElementById("puntuacion5");
  25.  
  26. f = document.getElementById("jugador6");
  27. f6 = document.getElementById("puntuacion6");
  28.  
  29. g = document.getElementById("jugador7");
  30. g7 = document.getElementById("puntuacion7");
  31.  
  32. h = document.getElementById("jugador8");
  33. h8 = document.getElementById("puntuacion8");
  34.  
  35. i = document.getElementById("jugador9");
  36. i9 = document.getElementById("puntuacion9");
  37.  
  38. j = document.getElementById("jugador10");
  39. j10 = document.getElementById("puntuacion10");
  40.  
  41. k = document.getElementById("jugador11");
  42. k11 = document.getElementById("puntuacion11");
  43.  
  44. l = document.getElementById("jugador12");
  45. l12 = document.getElementById("puntuacion12");
  46.  
  47. m = document.getElementById("jugador13");
  48. m13 = document.getElementById("puntuacion13");
  49.  
  50. n = document.getElementById("jugador14");
  51. n14 = document.getElementById("puntuacion14");
  52.  
  53. o = document.getElementById("jugador15");
  54. o15 = document.getElementById("puntuacion15");
  55.  
  56. z = document.getElementById("boton");
  57. z.addEventListener("click", sortear);
  58.  
  59.  
  60. }
  61.  
  62. function sortn(x,y)
  63. {
  64.  
  65. if(x>y)
  66.  
  67. return -1;
  68.  
  69. else if(x == y)
  70.  
  71. return 0;
  72.  
  73. else
  74.  
  75. return 1;
  76.  
  77. }
  78.  
  79. function sortear()
  80. {  
  81.  
  82.  
  83. var puntuaciones = new Array(15);
  84. puntuaciones[0] = (parseInt(a1.value)*1) + " = " + a.value.toUpperCase();
  85. puntuaciones[1] = (parseInt(b2.value)*1) + " = " + b.value.toUpperCase();
  86. puntuaciones[2] = (parseInt(c3.value)*1) + " = " + c.value.toUpperCase();
  87. puntuaciones[3] = (parseInt(d4.value)*1) + " = " + d.value.toUpperCase();
  88. puntuaciones[4] = (parseInt(e5.value)*1) + " = " + e.value.toUpperCase();
  89. puntuaciones[5] = (parseInt(f6.value)*1) + " = " + f.value.toUpperCase();
  90. puntuaciones[6] = (parseInt(g7.value)*1) + " = " + g.value.toUpperCase();
  91. puntuaciones[7] = (parseInt(h8.value)*1) + " = " + h.value.toUpperCase();
  92. puntuaciones[8] = (parseInt(i9.value)*1) + " = " + i.value.toUpperCase();
  93. puntuaciones[9] = (parseInt(j10.value)*1) + " = " + j.value.toUpperCase();
  94. puntuaciones[10] = (parseInt(k11.value)*1) + " = " + k.value.toUpperCase();
  95. puntuaciones[11] = (parseInt(l12.value)*1) + " = " + l.value.toUpperCase();
  96. puntuaciones[12] = (parseInt(m13.value)*1) + " = " + m.value.toUpperCase();
  97. puntuaciones[13] = (parseInt(n14.value)*1) + " = " + n.value.toUpperCase();
  98. puntuaciones[14] = (parseInt(o15.value)*1) + " = " + o.value.toUpperCase();
  99.  
  100.  
  101. var resultado = parseInt();
  102. var ganador = puntuaciones.sort(sortn());
  103. resultado = document.write(ganador);
  104. resultado;
  105.  
  106. }

Aclarar que intento capturar las puntuaciones ya obtenidas por otro sitio a traves de inputs en cajas de texto, ordenas las puntuaciones y luego mostrar las puntuaciones con sus respectivos jugadores.

Al ser sort, que separa los numeros por comas en una sola linea, tuve que adjuntar al array los nombres tambien, y creo que ese es el problema. Vereis que a la desesperada intente que el sort me interpretara solo nos numeros y puse multiplicaciones y parentesis para ver si le podia dar prioridad pero no funciona

Aunque cuando lo pruebo con ciertos numeros, sin rellenar las casillas de los nombres solo numeros, me los ordena mal. Por ejemplo con el 50 el 7 y el 70 me los ordena 50,7,70 :S

Gracias por vuestro tiempo y un saludo