1 2 var lista = null;3 var lista_array = ([]);4 5 window.addEventListener('load', function(event) {6 7 lista = document.getElementById('lista');8 9 fillList2();10 11 //order(methodA);12 13 }, true);14 15 function order(method, r=false) {16 lista_array.sort(method);17 if (r) lista_array.reverse();18 19 var p2o = ([]); // Position to order (item a goes to b)20 var i;21 for (i in lista_array) {22 p2o[lista_array[i].position] = i;23 }24 25 var k;26 var h=0;27 for (k in p2o) {28 //lista_array[i].style.top = (30*i)+'px';29 var end = p2o[k];30 var item = lista_array[end];31 item.innerHTML = 'Estoy en '+item.order+' y voy a '+i;32 item.style.marginTop = ((end-item.order-h)*30)+'px';33 h = (end-item.order);34 }35 }36 37 function methodA(a,b) {38 if (a.dataset['a'] < b.dataset['a']) {39 return -1;40 } else if (a.dataset['a'] > b.dataset['a']) {41 return 1;42 }43 return 0;44 }45 46 function methodB(a,b) {47 if (a.dataset['b'] < b.dataset['b']) {48 return -1;49 } else if (a.dataset['b'] > b.dataset['b']) {50 return 1;51 }52 return 0;53 }54 55 56 function methodAlfa(a,b) {57 if (a.innerHTML < b.innerHTML) {58 return -1;59 } else if (a.innerHTML > b.innerHTML) {60 return 1;61 }62 return 0;63 }64 65 function fillList2() {66 var div;67 68 var i;69 for (i=0; i<300; i++) {70 var a = i;71 var b = Math.ceil(Math.random()*100);72 div = document.createElement('div');73 div.order = i;74 div.position = i;75 div.dataset['a'] = a;76 div.dataset['b'] = b;77 div.innerHTML = 'Item '+a+' '+b;78 lista.appendChild(div);79 lista_array.push(div);80 }81 82 83 }84
Enlace
El enlace para compartir es: