Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class Prueba {
- static class Item implements Comparable<Item> {
- public Item(String somevalue, String somedate) throws ParseException {
- this.somevalue = somevalue;
- this.somedate = somedate;
- SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
- this.realDate = sdf.parse(somedate);
- }
- public String getSomevalue() {
- return somevalue;
- }
- public String getSomedate() {
- return somedate;
- }
- @Override
- public int compareTo(Item item) { //interfaz para ordenar.
- return realDate.compareTo(item.realDate);
- }
- @Override
- public String toString() {
- return "Item{" +
- "somevalue='" + somevalue + '\'' +
- ", somedate='" + somedate + '\'' +
- '}';
- }
- private String somevalue;
- private String somedate;
- private Date realDate;
- }
- public static void main(String[] args) throws ParseException {
- Stack<Item> asStack = new Stack<>();
- asStack.push(new Item("prueba1", "24/10/2010"));
- asStack.push(new Item("prueba2", "24/10/2005"));
- asStack.push(new Item("prueba3", "24/10/2007"));
- asStack.push(new Item("prueba4", "24/10/2017"));
- asStack.push(new Item("prueba5", "24/10/2001"));
- List<Item> listToOrder = new ArrayList<>();
- while(!asStack.isEmpty()) {
- listToOrder.add(asStack.pop()); //quitamos el ultimo...
- }
- Collections.sort(listToOrder); //order by comparable...
- for(Item i: listToOrder) {
- System.out.println(i);
- }
- //los vuelves a pushear....
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement