Originariamente inviato da alexcandy91
MA NON FUNZIONAAA..
Evidentemente hai sbagliato ad implementare l'algoritmo di ordinamento! Il problema non è dipeso dall'uso degli oggetti, anzi!
Ecco un esempio di implementazione corretto dell'algoritmo usando oggetti simili al tuo
codice:
public class Tester{
public int y;
/**
* Creates a new instance of <code>Tester</code>.
*/
public Tester(int y){
this.y = y;
}
@Override
public String toString(){
return y+"";
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Tester set[] = {new Tester(10), new Tester(3), new Tester(2), new Tester(5), new Tester(7)};
boolean swap = true;
while(swap){
swap = false;
for(int i = 0; i < set.length - 1; i++){
if( set[i].y > set[i+1].y ){
Tester x = set[i];
set[i] = set[i+1];
set[i+1] = x;
swap = true;
}
}
}
for(Tester t : set){
System.out.print(t + " ");
}
}
}