0

How do I sort a multidimensional array by one of the fields of the inner array?

In Java, How I can create a multidimensional array like this? and how I can sort it by X field? Any examples?

Array
(
    [0] => Array
        (
            [name] => Sony TV
            [price] => 600.00
        )

    [1] => Array
        (
            [name] => LG TV
            [price] => 350.00
        )

    [2] => Array
        (
            [name] => Samsung TV
            [price] => 425.00
        )  
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ephramd
  • 561
  • 2
  • 15
  • 41

2 Answers2

1

Use Arrays.sort to sort you multidimensional array. Assume Data is the custom class.

 Arrays.sort(data, new Comparator<Data[]>() {
            @Override
            public int compare(final Data[] data1, final Data[] data2) {
                final String name1 = data1[0].name;
                final String name2 = data2[0].name;
                return name1.compareTo(name2);
            }
        });

if you need each row's coulmn that is inner array to be sorted then you could sort it first.

for(Data[] data : datas){
   Arrays.sort(data, new Comparator<Data>() {
                @Override
                public int compare(final Data data1, final Data data2) {
                    final String name1 = data1.name;
                    final String name2 = data2.name;
                    return name1.compareTo(name2);
                }
            });
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

What you have doesn't look like a multidimensional array at all (well, it looks like a PHP multidimensional array, but not a Java one), but rather an array of objects, where each object has a "name" and a "price" field.

In these case you should first create your object's class. Let's call it Product:

public class Product implements Comparable<Product> {
    private String name;
    private BigDecimal price;

    public Product(final String name, final BigDecimal price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public int compareTo(final Product other) {
        return name.compareTo(other.name);
    }

    /* Other methods. */

    /* Getters and setters for price and name. */

}

You'll notice that this class implements Comparable<Product> and has a method compareTo. This means that a collection of products can be ordered. Then, you put your Products into an array and you use the Arrays.sort() method to sort it:

Product[] products = new Product[3];
products[0] = new Product("Sony TV", new BigDecimal("600.00"));
products[1] = new Product("LG TV", new BigDecimal("350.00"));
products[2] = new Product("Samsung TV", new BigDecimal("425.00"));
Arrays.sort(products);

Edit: I missed the fact that you wanted to sort by any field. In this case, you need Comparators. You'll need one per field:

Comparator<Product> nameComparator = new Comparator<Product>() {
    @Override
    int compare(final Product p1, final Product p2) {
        return p1.getName().compareTo(p2.getName());
    };
Comparator<Product> priceComparator = new Comparator<Product>() {
    @Override
    int compare(final Product p1, final Product p2) {
        return p1.getPrice().compareTo(p2.getPrice());
    };

Then, just give your comparators to the Array.sort() method:

 Arrays.sort(products, nameComparator);
 /* or */
 Arrays.sort(products, priceComparator);
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • Thanks for responding again. I understand how to do it by creating the class. But I would like to do so without defining the class "Product". Could implemented using HashMap? That is: `private static HashMap Object> Product String, String, Object> = new HashMap ();` – ephramd May 10 '13 at 10:16
  • @ephramd Yes, you can do it with a `Map`. Then you would have an array of maps and you define your comparators on `Map` objects rather than on `Product` objects. – Étienne Miret May 10 '13 at 10:19