Originariamente inviato da willybit
ciao pietro09,
negli array non esistono metodi di ricerca... bisogna farsi una funzioncina
Ti ringrazio. Nel frattempo ho trovato questa funzione sepolta nella mia libreria. La metto a volte che non serva a qualcuno
codice:
//-------------------------------------------------------------------------
//trova l'elemento x nel vettore non ordinato a in modo sequenziale
//restituisce l'indice dell'elemento trovato o -1 altrimenti
//-------------------------------------------------------------------------
function FindSequenziale(a, x)
{
var r = a.length;
var k = 0;
var y = '';
if(x == '') return -1;
x = x.toLowerCase( );
for(k = 0; k < r; k++)
{
y = a[k].toLowerCase();
if(x == y) return k;
}
return -1;
}
//-------------------------------------------------------------------------
//trova l'elemento che inizia per x nel vettore ordinato a
//restituisce l'indice dell'elemento trovato o -1 altrimenti
//-------------------------------------------------------------------------
function FindLeftBinario(a, x)
{
var l = 0;
var r = a.length - 1;
var w = 0, y = '', y1 = '', k = 0, k1 = 0;
if(x == '') return -1;
x = x.toLowerCase( );
while(l <= r)
{
k = Math.floor((l + r) / 2);
y = a[k].substr(0, x.length).toLowerCase();
if(x <= y) r = k - 1;
if(x >= y) l = k + 1;
if(x == y) w = 1;
}
if(w != 0)
{
k1 = k;
while (true)
{
k--;
if(k < 0) break;
y = a[k].substr(0, x.length).toLowerCase();
y1 = a[k1].substr(0, x.length).toLowerCase();
if(y != y1)
break;
else
k1 = k;
}
return k1;
}
else
return -1;
}