简体   繁体   English

Java 2D ArrayList和排序

[英]Java 2D ArrayList and sorting

I need to sort a shopping list by the aisle the item is located for example: 我需要按商品所在的过道对购物清单进行排序,例如:
[Bread] [1] [面包] [1]
[Milk] [2] [牛奶] [2]
[Cereal] [3] [谷物] [3]

I am planning to do this with ArrayList and was wondering how to make an 2D ArrayList Bonus questions: any ideas on how to sort by the aisle number? 我打算使用ArrayList进行此操作,并且想知道如何制作2D ArrayList奖励问题:关于如何按过道编号排序的任何想法?

Don't you have a class that holds your item + aisle information? 您没有开设可容纳您的物品和过道信息的课程吗? Something like: 就像是:

public class Item {
  private String name;
  private int aisle;

  // constructor + getters + setters 
}

If you don't, consider making one - it's definitely a better approach than trying to stick those attributes into ArrayList within another ArrayList. 如果不这样做,请考虑创建一个-与尝试将这些属性粘贴到另一个ArrayList中的ArrayList中相比,这绝对是一种更好的方法。 Once you do have said class, you'll either need to write a Comparator for your objects or make 'Item' Comparable by itself: 说完类后,您将需要为对象编写一个Comparator或使“ Item”本身可比较

public class Item implements Comparable<Item> {
  .. same stuff as above...

  public int compareTo(Item other) {
    return this.getAisle() - other.getAisle();
  }
}

Then all you do is invoke sort: 然后,您要做的就是调用sort:

List<Item> items = new ArrayList<Item>();
... populate the list ...
Collections.sort(items);

If you want to sort an ArrayList of ArrayLists then you can use the ColumnComparator . 如果要对ArrayList的ArrayList进行排序,则可以使用ColumnComparator

If you want to sort an ArrayList of your custom Object then you can use the BeanComparator . 如果要对自定义对象的ArrayList进行排序,则可以使用BeanComparator

I know the question was asked long ago but actually i had the same problem. 我知道这个问题是很久以前问过的,但实际上我有同样的问题。 If you dont know how many variables you would have on list but this is not a big number you could just implement comparator for every choose. 如果您不知道列表中有多少个变量,但这不是一个很大的数字,您可以为每个选择实现比较器。 eg 例如

I have ArrayList<ArrayList<Object>> and want to sort it by column's and i know that the nested list consist of variable number of objects i can just implement comparator for every possible value: 我有ArrayList<ArrayList<Object>>并想按列排序,并且我知道嵌套列表由可变数量的对象组成,我可以为每个可能的值实现比较器:

public class SecondColumnComparator implements Comparator {

public static boolean isNumeric(String str) {
    try {
        Integer integer = Integer.parseInt(str);
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

@Override
public int compare(Object o1, Object o2) {

    if (isNumeric(((ArrayList<String>) o1).get(1))) {

        Integer firstInteger = Integer.parseInt(((ArrayList<String>) o1).get(1));
        Integer secondInteger = Integer.parseInt(((ArrayList<String>) o2).get(1));

        return firstInteger.compareTo(secondInteger);

    }
    if (((ArrayList<Object>) o1).get(1) instanceof String) {

        String firstString = ((ArrayList<String>) o1).get(1);
        String secondString = ((ArrayList<String>) o2).get(1);

        return firstString.compareTo(secondString);
    }

    throw new Exception();
}

} }

And call this this way: 并这样调用:

        switch (valueSelected) {
        case 0:
            Collections.sort(this.listOfLists, new FirstColumnComparator());
            break;
        case 1:
            Collections.sort(this.listOfLists, new SecondColumnComparator());
            break;
        case 2:
            Collections.sort(this.listOfLists, new ThirdColumnComparator());
            break;
        case 3:
            Collections.sort(this.listOfLists, new FourthColumnComparator());
            break;
        default:

    }

In every comparator just modifying .get(x) where x is number of collumn by which you want sort. 在每个比较器中,只需修改.get(x) ,其中x是您要排序的列数。

The boolean isNumeric(String str); boolean isNumeric(String str); function may be used because you cant store different type of objects on one list so I put the recognition of this to the comparator and parse String to any other type. 之所以可以使用该函数,是因为您无法在一个列表上存储不同类型的对象,因此我将其识别结果提供给比较器,并将String解析为任何其他类型。

Remember that this comparator and its "calculations" are called to every single comparison made by algorithm so it is extremely inefficient... Despite this fact this is kind of sollution. 请记住,该comparator及其“计算”在算法进行的每个单个比较中都会调用,因此效率非常低...尽管有这个事实,但这是一种解决方案。

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

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