0

I have Class Customers and this class have attribute name : Customer_id this Customer_id is Random by this code :

  int USER_RANDOM_ID = rand.nextInt(2000) + 1;

Note : I stored all object in ArrayList and I need to push all Objects to Queues.

My question is how can I sort objects like this: customer with lowest id will be served first.

This is my code to Send Objects from List to Queue. Here I want to sort object dependent id number.

   AmmanQueue = new QueueImplementation();
            for(int i =0; i<MainActivity.Amman.getLength(); i++)
            {
                AmmanQueue.enqueue(MainActivity.Amman.getEntry(i));
            }
madhan kumar
  • 1,560
  • 2
  • 26
  • 36
saifmaher
  • 1
  • 1
  • Possible duplicate of [How to sort an arraylist of objects by a property?](https://stackoverflow.com/questions/2535124/how-to-sort-an-arraylist-of-objects-by-a-property) – F43nd1r May 27 '17 at 18:28
  • not duplicate my question is how i can move object from array to queue (Sorting ) – saifmaher May 27 '17 at 18:36

1 Answers1

0

You can use priority queue where you will pass custom comparator.So when you will en-queue the expected value will come from front.

//PriorityQueue example with Comparator
        Queue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator);


    //Comparator anonymous class implementation
    public static Comparator<Customer> idComparator = new Comparator<Customer>(){

        @Override
        public int compare(Customer c1, Customer c2) {
            return (int) (c1.getId() - c2.getId());
        }
    };
gati sahu
  • 2,576
  • 2
  • 10
  • 16