Q: Sort 2d array by 2nd dimension I need to sort my items depending on their price.
The price of each item is stored in a JSON array
I have created a 2d array to store the name and price
Something like this...
    String [][] priceArray = new String [itemArray.length()] [2];

    for(int i = 0; i < itemArray.length(); i++)
    {
        //put the item name in the first dimension
        priceArray[i][0] = itemArray.getJSONObject(i).getString("name");

        //put the item price in the second dimension        
        priceArray[i][1] = itemArray.getJSONObject(i).getString("baseprice");       
    }

    //DEBUG TO SEE THE RESULTS
    for(int i = 0; i < priceArray.length; i++)
    {
        Log.i("Item name : Item Price", priceArray[i][0] + " : " + priceArray[i][1]);               
    }

This works fine... but how can i sort the array by the price in the second dimension?
Is this even the best way to do this?

A: I suggest you create your own entity Price. And in your loop create List of Price entities with "name" and "baseprice" values. Then you can sort your list using Collections.sort()
and your own Comparator in wich you can specify way how to compare Price objects.

A: It seems that you're in object denial. Still, it's relatively easy to sort a two dimensional array on an arbitrary column. Just use a custom comparator. (Error checking for column existence is omitted in the snippet below.)
Arrays.sort(priceArray, new Comparator<String[]> {
    final int sortColumn = 1;

    @Override
    public int compare(String[] left, String right) {
        return left[sortColumn].compareTo(right[sortColumn]);
    }
});


A: You can try this
 int[][] array2D = {{1,1},{3,21},{5,8},{10,8}};

    Arrays.sort(array2D, new Comparator<int[]>() {
        public int compare(int[] first, int[] second) {
            return first[1] - second[1];
        }
    });


A: A better way to do this: take advantage of OO programming, first, create a class Item to contain name and price:
class Item {
    String name;
    BigDecimal price;

    public String toString() { return "ITEM: {name:" + name + ", price: " + price + "}" }
}

then create a list of Items rather than a 2D array:
List<Item> items = new ArrayList<Item>();

for(int i = 0; i < itemArray.length(); i++)
{
    Item item = new Item();
    item.name = itemArray.getJSONObject(i).getString("name");
    item.price = new BigDecimal(itemArray.getJSONObject(i).getString("baseprice"));
    items.add(item);
}

Finally, you can order by name if you want:
// Sort by name
Collections.sort(items, new Comparator<Item>() {

    @Override
    public int compare(Item o1, Item o2) {
        return o1.name.compareTo(o2.name);
    }

});

// at this point, items will be ordered by name

or if you want to order by price:
// Sort by price
Collections.sort(items, new Comparator<Item>() {

    @Override
    public int compare(Item o1, Item o2) {
        return o1.price.compareTo(o2.price);
    }
});

// at this point, items will be ordered by price

Of course, I'm missing some minor issues like using getters and setters instead of accessing the fields of Item directly, but the intention is to show the idea.
