Sairaj R
Sairaj R

Reputation: 1

Arranging values of an arrayList in a specifed order using java

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

Upvotes: 0

Views: 173

Answers (3)

JB Nizet
JB Nizet

Reputation: 691635

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()));
    }
});

Upvotes: 3

Sudhakar
Sudhakar

Reputation: 4873

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

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

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;
    }
});

Upvotes: 4

Related Questions