Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.*;
  4.  
  5. public class Prueba {
  6.     static class Item implements Comparable<Item> {
  7.         public Item(String somevalue, String somedate) throws ParseException {
  8.             this.somevalue = somevalue;
  9.             this.somedate = somedate;
  10.             SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  11.             this.realDate = sdf.parse(somedate);
  12.         }
  13.  
  14.         public String getSomevalue() {
  15.             return somevalue;
  16.         }
  17.  
  18.         public String getSomedate() {
  19.             return somedate;
  20.         }
  21.  
  22.         @Override
  23.         public int compareTo(Item item) { //interfaz para ordenar.
  24.             return realDate.compareTo(item.realDate);
  25.         }
  26.  
  27.         @Override
  28.         public String toString() {
  29.             return "Item{" +
  30.                     "somevalue='" + somevalue + '\'' +
  31.                     ", somedate='" + somedate + '\'' +
  32.                     '}';
  33.         }
  34.  
  35.         private String somevalue;
  36.         private String somedate;
  37.         private Date realDate;
  38.  
  39.     }
  40.     public static void main(String[] args) throws ParseException {
  41.         Stack<Item> asStack = new Stack<>();
  42.         asStack.push(new Item("prueba1", "24/10/2010"));
  43.         asStack.push(new Item("prueba2", "24/10/2005"));
  44.         asStack.push(new Item("prueba3", "24/10/2007"));
  45.         asStack.push(new Item("prueba4", "24/10/2017"));
  46.         asStack.push(new Item("prueba5", "24/10/2001"));
  47.  
  48.         List<Item> listToOrder = new ArrayList<>();
  49.         while(!asStack.isEmpty()) {
  50.             listToOrder.add(asStack.pop()); //quitamos el ultimo...
  51.         }
  52.  
  53.         Collections.sort(listToOrder); //order by comparable...
  54.         for(Item i: listToOrder) {
  55.             System.out.println(i);
  56.         }
  57.         //los vuelves a pushear....
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement