Comparison of Dates (Java.Util.Date) using compareTo and Java Generic

0

I'm developing an application of the Facebook genre.

The problem is in sorting Publications by Date. My program uses Generics and a DoubleLinkedOrdeneredList to store those publications.

  @Override
    public void addElement(T element) {

            Comparable temp;

            temp = (Comparable) element;

            DoubleNode<T> traverse = this.getHead();
            DoubleNode<T> newnode = new DoubleNode<>(element);

            if (isEmpty()) {

                this.setHead(newnode);
                this.setTail(newnode);
            } else if (temp.compareTo(this.getTail().getElement()) >= 0) {

                this.getTail().setNext(newnode);
                newnode.setPrevious(this.getTail());
                newnode.setNext(null);

                this.setTail(newnode);
            } else if (temp.compareTo(this.getHead().getElement()) <= 0) {
                this.getHead().setPrevious(this.getTail());
                newnode.setNext(this.getHead());
                newnode.setPrevious(null);
                this.setHead(newnode);
            } else {
                while ((temp.compareTo(traverse.getElement()) > 0)) {
                    traverse = traverse.getNext();
                }

                newnode.setNext(traverse);
                newnode.setPrevious(traverse.getPrevious());
                traverse.getPrevious().setNext(newnode);
                traverse.setPrevious(newnode);
            }

        }

My question is how we can pass an object of type Message into the addElement method and then using an attribute of type Date (java.util) compare the dates and sort.

    
asked by anonymous 16.02.2018 / 21:46

0 answers