0

I have 5 objects that represent football teams. The class code is this:

package Football;

public class Equipo{
private String nombreEquipo;
private int goles, puntos;

Equipo(){
    nombreEquipo = "";
    goles = 0;
    puntos = 0;
}

public void setNombreEquipo(String dato){nombreEquipo = dato;}
public void setGol(int dato){goles = dato;}
public void setPuntos(int dato){puntos = dato;}
public String getEquipo(){return nombreEquipo;}
public int getGoles(){return goles;}
public int getPuntos(){return puntos;}
}

Suppose that I created 5 objects of the class "Equipo" (or football Team):

Equipo T1 = new Equipo(), T2 = new Equipo(), T3 = new Equipo(), T4= new Equipo(), T5= new Equipo();

Then I inserted each team in my ArrayList:

listaEquipos.add(T1);
listaEquipos.add(T2);
listaEquipos.add(T3);
listaEquipos.add(T4);
listaEquipos.add(T5);

And then , using a set method , I insert the points that the team earned:

   listaEquipos.get(0).setPuntos(45);
   listaEquipos.get(1).setPuntos(34);
   listaEquipos.get(2).setPuntos(20);
   listaEquipos.get(3).setPuntos(25);
   listaEquipos.get(4).setPuntos(30);

Well, what I want to know is:

How i can order these items inside of my ArrayList from highest to lowest amount of points, using my function getPuntos ?

In this example , should be placed in this order:

listaEquipos(0) = 45
listaEquipos(1) = 34
listaEquipos(4) = 30
listaEquipos(3) = 25
listaEquipos(2) = 20

¿How I can accomplish this?

TwoDent
  • 405
  • 7
  • 26
  • 1
    Possible duplicate of [Sorting a collection of objects](http://stackoverflow.com/questions/1206073/sorting-a-collection-of-objects). – rgettman Dec 12 '15 at 00:40

1 Answers1

1

In Java 8 you can do

// sort by putos from highest to lowest.
listaEquipos.sort(Comparator.comparing(Equipo::getPutos).reversed());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130