4

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?

Tala
  • 8,888
  • 5
  • 34
  • 38
Louis Evans
  • 671
  • 2
  • 8
  • 18
  • why the android tag? you realize that adding unrelated tags can cause people to ignore your post? – Majid Laissi Aug 15 '13 at 09:51
  • See these links for related questions http://stackoverflow.com/questions/4158679/sorting-a-2-dimensional-array http://stackoverflow.com/questions/4907683/sort-a-two-dimensional-array-based-on-one-column http://stackoverflow.com/questions/10321123/need-help-sorting-two-dimensional-arrays-by-second-element-and-then-by-first-ele – Paul Renton Aug 15 '13 at 09:51
  • @Majid L It is actually for an android app...I will make this clearer in future. – Louis Evans Aug 15 '13 at 09:52

4 Answers4

1

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.

x90
  • 2,140
  • 15
  • 25
1

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]);
    }
});
Community
  • 1
  • 1
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
1

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];
        }
    });
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

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.

morgano
  • 17,210
  • 10
  • 45
  • 56
  • I have tried exactly this...but when i print the results to test, I get: "{packagename}.ItemList$Item@41f6ba68" <-- the last number changes – Louis Evans Aug 15 '13 at 10:22
  • I updated my answer, I added `toString()` to `Item` so when you use `System.out.println()` it prints a more friendly result – morgano Aug 15 '13 at 10:27
  • I have figured out what i was doing wrong now...it works how i wanted it to now. Thanks! – Louis Evans Aug 15 '13 at 13:04