ephramd
ephramd

Reputation: 380

Sort a multidimensional array by one of the fields of the inner array

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

Upvotes: 0

Views: 1099

Answers (2)

Étienne Miret
Étienne Miret

Reputation: 6650

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

Upvotes: 1

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

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

Upvotes: 1

Related Questions