简体   繁体   中英

Java 2D ArrayList and sorting

I need to sort a shopping list by the aisle the item is located for example:
[Bread] [1]
[Milk] [2]
[Cereal] [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?

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. Once you do have said class, you'll either need to write a Comparator for your objects or make 'Item' Comparable by itself:

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:

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 .

If you want to sort an ArrayList of your custom Object then you can use the 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:

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.

The 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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