Results 1 to 3 of 3
  1. #1
    Stavrosgr is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Create a function that sorts an arraylist of objects

    Hey , i need help with creating a function that sorts objects in an ascending order in the class ClientQueue.I've tried to do a lot of times but all i get is erros...The function is called addClient and,everytime it's called, it has to add a Client object in the correct order in the arraylist.The class Client contains a variable called priority and the arraylist is sorted according to the priority of the clients.The related codes are the following;
    Java Code:
    public class Cashier {
    
        private boolean open;
        private Client queue;
        Client currentClient;
        int serviceTime;
        int idleTime;
    
        public Cashier(boolean open) {
            this.open=open;
        }
    Java Code:
    import java.util.Random;
    public class Client {
        public int priority;
        private int numItems;
        private double paysCash;
        
        public Client(int i) {
        int creationTime=i;
        int MAX_TIME=43200;
        Random rnd=new Random(); 
        numItems=rnd.nextInt(49)+1; //plh8os eidwn
        final int P_PAYS_CASH=rnd.nextInt(10);
        int P_IS_SPECIAL=rnd.nextInt(10);
        if(P_IS_SPECIAL>1) {
            priority=creationTime;
        }
        else {
                priority=creationTime-MAX_TIME;
            }
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Create a function that sorts an arraylist of objects

    You have no ClientQueue class.
    There is no array.
    Or even an attempt at the addClient() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Create a function that sorts an arraylist of objects

    When you have done what Tolls said, look up implementing comparable, compareTo(x other) and Collections.sort() this will give you your answer.
    Last edited by monkeyjr97; 12-03-2012 at 06:36 PM.

Similar Threads

  1. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  2. recursive function to create many objects?
    By dacoolest in forum New To Java
    Replies: 1
    Last Post: 11-22-2011, 08:03 PM
  3. Trying to create an undo function using an arraylist.
    By Spirit356 in forum New To Java
    Replies: 6
    Last Post: 04-27-2010, 07:01 PM
  4. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 08:38 PM
  5. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •