简体   繁体   English

按第二维排序2d数组

[英]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 每个项目的价格都存储在JSON数组中

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? 这甚至是最好的方法吗?

I suggest you create your own entity Price. 我建议你创建自己的实体Price。 And in your loop create List of Price entities with "name" and "baseprice" values. 并在您的循环中创建具有“name”和“baseprice”值的价格实体列表。 Then you can sort your list using Collections.sort() and your own Comparator in wich you can specify way how to compare Price objects. 然后,您可以使用Collections.sort()和您自己的Comparator对列表进行排序,您可以指定如何比较Price对象。

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

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 better way to do this: take advantage of OO programming, first, create a class Item to contain name and price: 更好的方法:利用OO编程,首先,创建一个包含名称和价格的类Item:

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: 然后创建一个Items列表而不是一个2D数组:

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. 当然,我错过了一些小问题,比如使用getter和setter而不是直接访问Item的字段,但目的是展示这个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM