Java 2D ArrayList and sorting

前端 未结 3 1451
执笔经年
执笔经年 2021-01-22 22:52

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

相关标签:
3条回答
  • 2021-01-22 23:24

    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.

    0 讨论(0)
  • 2021-01-22 23:33

    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.

    0 讨论(0)
  • 2021-01-22 23:47

    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);
    
    0 讨论(0)
提交回复
热议问题