|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Feb 2009
Messaggi: 700
|
[JAVA] Ordinare lista di oggetti in base ad uno specifico campo
Ciao,
se io ho una lista di oggetti di tipo TaskDTO dichiarata così: Codice:
List<TaskDTO> listaTaskUrgenti = new ArrayList<TaskDTO>(); Ogni oggetti TaskDTO ha una serie di campi tra cui una data: Codice:
public class TaskDTO extends AbstractBaseDTO {
/**
*
*/
private static final long serialVersionUID = 1L;
private String activityType;
private String task;
private Date dataCreazione;
private String cliente;
private String operatoreFO;
private String operatoreBO;
private boolean urgente;
.......
METODI GETTER e SETTER
}
Grazie Andrea |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Collection.sort() passando come argomenti la tua lista e un Comparator per TastDTO da te definito, che compara le date.
Il Comparator lo definisci (ad esempio) così: Codice:
class ByDateComparator implements Comparator<TaskDTO>
{
public static enum Order {
ASC, DESC
}
public final Order oder;
public ByDateComparator(Order chosen) {
oder = chosen;
}
int compare(TaskDTO a, TaskDTO b) {
switch (order) {
case ASC :
return a.getDate().compareTo(b.getDate());
case DESC :
return b.getDate().compareTo(a.getDate());
}
}
boolean equals(Object obj) {
if (this == obj) return true;
return obj instanceof ByDateComparator &&
this.order == ((BydateComparator)obj).order;
}
}
Scritto tutto al volo senza passare da un editor, percui verifica...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 29-07-2011 alle 17:29. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: May 2004
Città: Londra (Torino)
Messaggi: 3692
|
Certo che il confronto con i competitor piu' moderni e' impietoso.
__________________
Se pensi che il tuo codice sia troppo complesso da capire senza commenti, e' segno che molto probabilmente il tuo codice e' semplicemente mal scritto. E se pensi di avere bisogno di un nuovo commento, significa che ti manca almeno un test. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Hai ragione, ma non vedo che c'entri col topic...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:22.




















