7 Replies - 197 Views - Last Post: 12 July 2020 - 03:58 AM Rate Topic: -----

#1 O'Niel   User is offline

  • D.I.C Addict

Reputation: 26
  • View blog
  • Posts: 598
  • Joined: 13-September 15

Sort treeset of objects by date property

Posted 11 July 2020 - 12:05 PM

I have the following class:
public class iOSUpdate implements Comparable<iOSUpdate> {
    // Properties
    private LocalDate releaseDate;  // Date when the update is released

    // Default constructor
    public iOSUpdate() {
        this("Unkown", "", 0.0, 0, null, LocalDate.now());
    }

    // ReleaseDate
    public void setReleaseDate(LocalDate releaseDate) {
        if (releaseDate.isAfter(LocalDate.now())) {
            throw new IllegalArgumentException("Releasedate can't be in the future.");
        }

        this.releaseDate = releaseDate;
    }
    public LocalDate getReleaseDate() { return this.releaseDate; }

    public int compareTo(LocalDate d1, LocalDate d2) {
        return d1.compareTo(d2);
    }

}




And another class, which is just a container for a treeset holding all the objects (a multiclass).
package be.kdg.iosupdates.model;

import javax.swing.text.html.HTMLDocument;
import java.time.LocalDate;
import java.util.*;

public class iOSUpdates {
    // TreeSet
    TreeSet<iOSUpdate> t = new TreeSet<iOSUpdate>();

    // Method to add iOSUpdate
    public boolean voegToe(iOSUpdate iosupdate) {
        return this.t.add(iosupdate);
    }

    // Method to search a specific iOSUpdate
    public iOSUpdate zoek(String name) {
        Iterator<iOSUpdate> iterator = this.t.iterator();

        while (iterator.hasNext()) {
            if (iterator.next().getName() == name) {
                return iterator.next();
            }
        }

        return null;
    }

    // Method to delete a specific iOSUpdate
    public boolean verwijder(String name) {
        iOSUpdate toDelete = zoek(name);

        if (toDelete == null) {
            return false;
        }

        if (this.t.remove(toDelete)) {
            return true;
        }

        return false;
    }

    // Method to get amount of updates
    public int getAantal() {
        return this.t.size();
    }

    // How to do this one???
    public List<iOSUpdate> gesorteerdOpReleaseDate() {
        Collections.sort(this.t, (x, y) -> );
    }
}




So there will be multiple instances of iOSUpdate inside the treeset which is in iOSUpdates.
The function gesorteerdOpReleaseDate() should return a list of all the iOSUpdate objects in the treeset.

I found this:
Collections.sort(list, (x, y) -> x.startDate.compareTo(y.startDate));




And tried to replace startDate with getReleaseDate() and just releaseDate. But it said those weren't found...

Is This A Good Question/Topic? 0
  • +

Replies To: Sort treeset of objects by date property

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 836
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Sort treeset of objects by date property

Posted 11 July 2020 - 12:24 PM

The objects in a TreeSet can be ordered by a Comparator passed to the TreeSet's constructor.

This post has been edited by NormR: 11 July 2020 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Sort treeset of objects by date property

Posted 11 July 2020 - 01:03 PM

You've implemented compareTo incorrectly. See API docs for Comparable
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 836
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Sort treeset of objects by date property

Posted 11 July 2020 - 01:20 PM

What compiler errors are you getting with the posted code?
Was This Post Helpful? 0
  • +
  • -

#5 O'Niel   User is offline

  • D.I.C Addict

Reputation: 26
  • View blog
  • Posts: 598
  • Joined: 13-September 15

Re: Sort treeset of objects by date property

Posted 11 July 2020 - 05:15 PM

Is this getting closer?

public List<iOSUpdate> gesorteerdOpReleaseDate() {
        List<iOSUpdate> l = new ArrayList<>();
        l.addAll(this.t);
        Collections.sort(l, new Comparator<iOSUpdate>() {

            public int compare(iOSUpdate u1, iOSUpdate u2) {
                return u1.getReleaseDate().compareTo(u2.getReleaseDate());
            }

        });
        
        return l;
    }


Was This Post Helpful? 0
  • +
  • -

#6 O'Niel   User is offline

  • D.I.C Addict

Reputation: 26
  • View blog
  • Posts: 598
  • Joined: 13-September 15

Re: Sort treeset of objects by date property

Posted 11 July 2020 - 06:03 PM

I'm not getting any errors; it just ain't working.

This are all my sort functions.

   //3 Sort methods
    public List<iOSUpdate> gesorteerdOpName() {

        List<iOSUpdate> l = new ArrayList<>();
        l.addAll(this.t);

        Collections.sort(l, new Comparator<iOSUpdate>() {

            public int compare(iOSUpdate u1, iOSUpdate u2) {
                String s1 = u1.getName();
                String s2 = u2.getName();

                String alphabet = "abcdefghijklmnopqrstuvwxyz";

                int x = 0;

                int i = 0;
                int j = 0;

                while (i != j) {
                    if (x > s1.length() || x > s2.length())
                        return 0;

                    char c = alphabet.charAt(x);

                    for (i = 0; i < alphabet.length(); i++) {
                        if (alphabet.charAt(i) == c)
                            break;
                    }
                    for (j = 0; j < alphabet.length(); j++) {
                        if (alphabet.charAt(j) == c)
                            break;
                    }

                    x++;
                }

                return j - i;
            }

        });

        return l;
    }
    public List<iOSUpdate> gesorteerdOpRelease() {
        List<iOSUpdate> l = new ArrayList<>();
        l.addAll(this.t);

        Collections.sort(l, new Comparator<iOSUpdate>() {

            public int compare(iOSUpdate u1, iOSUpdate u2) {
                return u1.getRelease() - u2.getRelease();
            }

        });

        return l;
    }
    public List<iOSUpdate> gesorteerdOpReleaseDate() {
        List<iOSUpdate> l = new ArrayList<>();
        l.addAll(this.t);
        Collections.sort(l, new Comparator<iOSUpdate>() {

            public int compare(iOSUpdate u1, iOSUpdate u2) {
                return u1.getReleaseDate().compareTo(u2.getReleaseDate());
            }

        });

        return l;
    }
}




Name is working, release kinda, but releaseDate is just not correct at all.
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 836
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: Sort treeset of objects by date property

Posted 11 July 2020 - 06:15 PM

Quote

it just ain't working.

Can you print out some results that shows what is happening?
Add some comments that describe where the output is wrong.

Also please post the driver code that calls the sort methods. The code should be complete to allow compiling and executing for testing.

This post has been edited by NormR: 11 July 2020 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Sort treeset of objects by date property

Posted 12 July 2020 - 03:58 AM

Quote

return u1.getRelease() - u2.getRelease();

What's that? You don't even have such a method in your posted code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1