4

I have a list of HashMaps. Each HashMap consists of several kay-value pairs and everything comes as a string. I am storing all the hashmaps inside an arraylist. Now I need to sort the arraylist based on the key inside the hashmap.

Here is my sample data:

{
 "productID":"5643",
 "productName":"Apple - iPod touch",
 "outsidePrice":"189.99", 
 "merchantID":"134439",
 "ourPrice":"184.99", 
 "storeName":"Ebay",
}


{
 "productID":"3243",
 "productName":"Apple - iPad",
 "outsidePrice":"389.99", 
 "merchantID":"54439",
 "ourPrice":"384.99", 
 "storeName":"Apple",
}

I am storing this data inside this structure.

ArrayList<HashMap<String, String>> data_list = new ArrayList<HashMap<String, String>>();

I have a huge list of items like this. Now I need to sort the arraylist based on the productName, Price, storeName, productID fields inside the hashmap.

2
  • "I have a huge list of items like this". You should be storing this in SQLite. Commented Nov 25, 2012 at 7:20
  • I don't need that. Say the list has 25 item details like this. The data will be keep on changing and it is temporary. It won't occupy much memory. Commented Nov 25, 2012 at 7:23

3 Answers 3

7

I recommend that you use a custom product class to do this for you. It will ultimately make your code easier to maintain and more robust, IMHO.

How about this?

A class to represent your data:

class Product{

     public string productId;
     public string productName;
     public BigDecimal outsidePrice;
     public int merchantId;
     public BigDecimal ourPrice;
     public string storeName;

// whatever constuctors you need

}

A List of your products:

  List<Product> products;

Now define a Comparator to sort, one for each field that you need to sort on. Here is an example for productId.

public class ProductProductIdComparator implements Comparator<Product>{

     @Override
     public int compare(Product product1, Product product2) {

        if (product1.productId > product2.productId){
            return +1;
        }else if (product1.productId < product2.productId){
            return -1;
        }else{
            return 0;
        }
    }
}

And finally, a Collections sort which accepts a comparator as an argument:

Collections.sort(products, new ProductProductIdComparator());
Sign up to request clarification or add additional context in comments.

1 Comment

This is a good approach. Since Java should be OOP, Entity classes are to favor against String Lists
6

The Collections class provides a utility method for sorting a list in place, using a Comparator.

final List<Map<String, String>> dataList = new ArrayList<HashMap<String, String>>(4);
Collections.sort(dataList, new Comparator<Map<String, String>>() {
    @Override
    public int compare(final Map<String, String> map1, final Map<String, String> map2) {
        // Get fields from maps, compare
    }
}

1 Comment

I will try it and come back
-1

You can use arrays.sort() to achieve this in short, at some minor memory cost.

HashMap[] result = Arrays.sort(list.toArray(), new Comparator() {
  public void compare(Object o1, Object o2) {
    HashMap<String, String> a = (HashMap<String, String>)o1;
    HashMap<String, String> b = (HashMap<String, String>)o2;
    // return value as per contract of Comparator.compare() doing whatever comparisons you need.
  }

  public boolean equals(Object obj) { return this == obj; }
});

4 Comments

I don't understand the answer. I don't have just two sets of data. I have 25 sets of hashmap inside my arraylist.
What this does is first convert the ArrayList to an array with toArray(). It then uses the built in sort in the Arrays class. Arrays.sort() runs a merge sort, comparing each pair of elements by calling the comparator you are passing in. So basically, your comparator just needs to return -1 if o1 is before o2, 0 if they are they same, and 1 if o1 is after o2. Arrays.sort() will take care of the rest.
This code does not work. 1) The Arrays method does a sort in place, you can't assign a value from calling it. 2) The original collection is unaffected by your call, since you are not sorting it, but an array created from it.
Why convert to array, Collection.sort` does then same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.