2017-05-27 34 views
0

我有類客戶和這個類有屬性名稱:CUSTOMER_ID 這CUSTOMER_ID是此代碼隨機:我如何使用ID對隊列中的客戶進行排序?

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

注:我存儲在ArrayList中的所有對象,我需要所有的對象推到隊列。

我的問題是我如何排序這樣的對象: 客戶與最低ID將首先服務。

這是我的代碼從列表發送對象到隊列。在這裏我想排序對象相關的ID號碼。

AmmanQueue = new QueueImplementation(); 
      for(int i =0; i<MainActivity.Amman.getLength(); i++) 
      { 
       AmmanQueue.enqueue(MainActivity.Amman.getEntry(i)); 
      } 
+0

的可能的複製【如何通過性能對象的ArrayList排序?](https://stackoverflow.com/questions/2535124/how-to-sort-an-arraylist-of-objects-by -a財產) – F43nd1r

+0

不重複我的問題是我如何將對象從數組移動到隊列(排序) – saifmaher

回答

0

您可以使用優先級隊列,您將傳遞自定義比較器。因此,當您將排隊期望值將來自前面。

//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()); 
     } 
    }; 
相關問題