Java - Ordenar array de objetos

 
Vista:

Ordenar array de objetos

Publicado por Ru (35 intervenciones) el 28/10/2020 21:58:10
Hola buenas tardes, tengo un problema para ordenar un array de objetos por el atributo TS, si alguien me puede ayudar se lo agradeceria:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.Serializable;
 
public class Proceso implements Comparable<Proceso>{
 
	int PID; //proces id
	int TS=0;  //tiempo de servicio
 
 
		public void setPID(int id){
		    this.PID=id;
		  }
 
	  public void setTS(int t){
		    this.TS=t;
		  }
 
	  public void setTSinicial(int t) {
		  this.TSinicial=t;
	  }
	  public int getPID(){
		    return this.PID;
		  }
 
 
	  public int getTS(){
		    return this.TS;
		  }
 
 
 
	@Override
	public int compareTo (Proceso o) {
        if (o.getTS() > t) {
            return -1;
        } else if (o.getTS()>t) {
 
            return 0;
        } else
        return 1;
    }
 
 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
 
public class SJF {
 
 
 
	static ArrayList<Proceso> lista2 = new ArrayList();
	 static int c=0;
	static int q=0;
	 static int r=0;
 
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
		ingresarDatos();
		ordenarLista2();
		mostrarDatos2();
	}
 
 
    public static void ingresarDatos() {
		System.out.println("**********************************************");
		System.out.println("   Ingrese cantidad de procesos a ingresar    ");
		System.out.println("**********************************************");
		Scanner cant = new Scanner(System.in);
		int num=cant.nextInt();
 
 
		System.out.println("**********************************************");
		System.out.println("   Ingrese valor de Quantum    ");
		System.out.println("**********************************************");
		Scanner quantum = new Scanner(System.in);
		 q=quantum.nextInt();
 
 
		for (int i=0; i<num;i++) {
		Scanner teclado = new Scanner(System.in);
		System.out.println("************************************");
		System.out.println("   Ingrese Tiempo de servicio del proceso: "+i+ "   ");
		System.out.println("************************************");
		int numero = teclado.nextInt();
		teclado.nextLine();
		System.out.println("**********************************");
		System.out.println("   Ingrese el tiempo de llegada   ");
		System.out.println("***********************************");
		Scanner teclado2 = new Scanner(System.in);
		int numero2=teclado.nextInt();
		Proceso aux = new Proceso();
		aux.setPID(c);
		aux.setTS(numero);
		aux.setTSinicial(numero);
		lista2.add(aux);
		c++;
		}
    }
 
    public static void ordenarLista2() {
        Collections.sort(lista2);
 
    }
 
    public static void mostrarDatos2() {
        for (int i=0; i<lista2.size(); i++) {
        System.out.println(" ");
        System.out.println("*************************************");
        System.out.println("     ID proceso: "+lista2.get(i).getPID());
        System.out.println("     Tiempo de servicio: "+lista2.get(i).getTS());
        System.out.println("*************************************");
        System.out.println(" ");
 
 
 
        }
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Kabuto
Val: 3.428
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Ordenar array de objetos

Publicado por Kabuto (1381 intervenciones) el 29/10/2020 01:30:20
Está mal escrito el método compareTo(), prueba con este:

1
2
3
4
5
6
7
8
9
@Override
public int compareTo (Proceso o) {
    if (o.getTS() > t) {
        return -1;
    } else if (o.getTS() < t) {
        return 1;
    } else
        return 0; //Son iguales
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar