0

Any ideas on how to apply Collections.sort method to sort my arraylist by priority of each grocItem object within the itemData ArrayList?

public class GroceryProgram {

    private final static int GROC_SIZE = 6;
    private final List<ItemData> itemData = new ArrayList<ItemData>();

    private void setUpList() {

        Scanner keyboard = new Scanner(System.in);

        for (int i = 0; i < GROC_SIZE; i++)  {

           System.out.print("\nEnter item name (" + i + ") : ");               
           String name = keyboard.next();

           System.out.print("\nEnter the price of item (" + i + ") : ");
           double cost = keyboard.nextDouble();

           System.out.print("\nEnter Priority Number (" + i + ") : ");
           int priority = keyboard.nextInt();

           ItemData grocItem = new ItemData(name, cost, priority);
           itemData.add(grocItem); // add grocery items to itemData ArrayList

           Collections.sort(grocItem);
           for (Int priority : priority) {
               System.out.println(integer);

3 Answers 3

2

Call sort() with a Comparator. For example, a Comparator in ascending-order of priority could look like this.

Collections.sort( items, new Comparator<ItemData>() {
    public int compare (ItemData o1, ItemData o2) {
        int comp = o1.getPriority() - o2.getPriority();
        return comp;
    }
});

PS: 'itemData' is bad variable naming -- it would refer to a single item, not a list. 'groceryItems', 'stockItems' or 'itemList' would be better.

Variable names should enable you to speak in meaningful, clear, concise English about your software.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Could you use Collections.sort(List list, Comparator c) to deal with this? You could simply do the following :

Collections.sort(itemData, new Comparator() {
  public int compare(Object o1, Object o2) {
    // Return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
  }
}

Comments

0

You can use custom comparator as below:

Collections.sort(itemData ,new PriorityComparator());

        //print sorted arraylist,it will print the data in ascending order (low priority->high priority).feel free to modify if you want to go for descending order
        System.out.println(itemData );

PriorityComparator class definition:

private static class PriorityComparator implements Comparator{

    @Override
    public int compare(ItemData object1, ItemData object2) {
        return (object1.priority< object2.priority) ? -1: (object1.priority> object2.priority) ? 1:0 ;
    }

}

Reference:

how-to-sort-arraylist-in-java-example

I hope it will be helpful !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.