0

I was trying to find a better and optimized solution for the following scenario
I have an arrayList

ArrayList<Order> orders = new ArrayList<Order>();  

and the sorted result values in orders are as follows

    {  
     medicine,    
     medicine,  
     milk,    
     milk,    
     pillow,  
     Soap,         
     toy       
}  

and I have an ENUM for all these items which is

public Enum Items{  
PILLOW("pillow"),  
HARDDISK("harddisk"),  
MILK("milk"),  
SOAP("soap"),  
MEDICINE("medicine"),  
TOY("toy")    
}

My output order should be as follows

{    
milk,  
milk,  
harddisk,  
medicine,  
medicine,  
toy,  
soap,  
pillow  
}  

In order to accomplish this all i was trying to do is

ArrayList<Order> resultList = new ArrayList<Order>();  
for(Order order: orders){  
if(order.getItemName.equals(Items.MILK){  
resultList.add(order);  
}    

for(Order order: orders){    
if(order.getItemName.equals(Items.MEDICINE){  
resultList.add(order);    
}  

.......  

for(Order order: orders){  
if(order.getItemName.equals(Items.PILLOW){  
resultList.add(order);  
}  

If i follow the above approach, i am able to get the output order in the above specified order, but my concern is since i am looping many for loops here, I want suggestions for optimized approach.

output should be sorted in the based on the following order

1) milk 2) harddisk 3) medicine 4) toy 5) soap 6) pillow

Sairaj R
  • 1
  • 1
  • 3
    Why would you expect that output? It's not the natural sorting. On what basis you want to perform the sorting? – Rohit Jain Feb 16 '13 at 18:47
  • This answer addresses enums with custom Comparators: http://stackoverflow.com/a/519844/1598965 – Eric P. Feb 16 '13 at 18:51

3 Answers3

4

Instead of that you can use Collections#sort(list,comparator) where you can pass your custom comparator to get the result in certain order.

Collections.sort(list, new Comparator<Order>() {

    @Override
    public int compare(Order obj1, Order obj2) {
         // write the custom logic 
         // a negative integer, zero, or a positive integer as the first argument
         // is less than, equal to, or greater than the second
         return 0;
    }
});
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
3

Enums have a natural order, which is the order of the declaration of the enum members. So if you want the order you specify, the enum should be declared as:

public Enum Item {  
    MILK("milk"),  
    HARDDISK("harddisk"),  
    MEDICINE("medicine"),  
    TOY("toy"),
    SOAP("soap"),  
    PILLOW("pillow");    
}

And then you could sort your orders like this:

Collections.sort(orders, new Comparator<Order>() {
    @Override
    public int compare(Order o1, Order o2) {
        return o1.getItem().compareTo(o2.getItem());
    }
}); 

If you can't modify the enum, then use the following trick:

private static final List<Item> ITEMS_IN_ORDER = Arrays.asList(new Item[] {
    Item.MILK,  
    Item.HARDDISK,  
    Item.MEDICINE,  
    Item.TOY,
    Item.SOAP,  
    Item.PILLOW
});

...

Collections.sort(orders, new Comparator<Order>() {
    @Override
    public int compare(Order o1, Order o2) {
        return Integer.compare(ITEMS_IN_ORDER.indexOf(o1.getItem()),
                               ITEMS_IN_ORDER.indexOf(o2.getItem()));
    }
});
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I appreciate it, but in my scenario this enum is generic i mean it is used in other modules as well, i cannot make it working for one module – Sairaj R Feb 16 '13 at 18:59
0

Well one alternative ,modify your enum class to

public Enum Items{ 
 MILK("milk",1),     
 HARDDISK("harddisk",2),
 MEDICINE("medicine",3),  
 TOY("toy",4)  
 SOAP("soap",5),  
 PILLOW("pillow",6), 

 private String name;
 private sortValue;


Items(double name, double sortValue) {
    this.name= name;
    this.sortValue= sortValue;
} 
}

use a comparator, to sort based on the sort value

Collections.sort(list,new Comparator<Fruit>() {

        public int compare(Items item1, Items item2) {
                   return item1.sortValue - item2.sortValue;

});

one advantage it has over JB Nizet is it doesnt depend on the order in which you declare values in the Enum class

Sudhakar
  • 4,823
  • 2
  • 35
  • 42
  • good..but constraint is that i am making use of a jar file of others...i shouldn't make any changes to their existing code...All i want implement things in better from my side – Sairaj R Feb 16 '13 at 19:04