Search code examples
javaclasspriority-queue

Priority quene based on argument of person class


Hey I've class called person that looks like below

PersonClass[] person= {
            new PersonClass(90234234434L, "Name", "Surname", Age, "Street", Priority),
            ...
            ...
    };

Then I create array

 ArrayDeque<String> arrayDeque = new ArrayDeque<>();

edit: my mistake, I'm already using

PriorityQueue<String> priorityQueue = new PriorityQueue<>

I tried many ways but I cant solve, how to sort this person array by priority value. If something is unclear, ask. Thanks for help in advance.


Solution

  • You can sort your PersonClass objects using Comparable.

    First you have to implements Comparable in your PersonClass and override the compareTo method like below,

    @Override
    public int compareTo(PersonClass person) {
        return this.Priority - person.Priority;
    }
    

    Then you can sort the person array in your method using sort method in Arrays

    Arrays.sort(array)