简体   繁体   中英

Doing a Selection sort on an ArrayList

I need to sort the array list from highest to lowest based on the "value" and im really stuck :( basically in this project, they are gonna run a list of items and this method is supposed to put the one with the highest value first and so fort and im trying to use a selection sort. Thank for you help in advance :) here is what i have at the moment

public void pickMostExpensiveFirst(ArrayList<Item> totalListOfItems)
{
    int max, i ,j;
    Item temp;

    for (i = 0; i < totalListOfItems.size() - 1; i++)
    {
        max = i;

        for (j = i + 1; j < totalListOfItems.size(); j++)
        {
            if (totalListOfItems.get(max).getValue()
                    .compareTo(totalListOfItems.get(j).getValue()) > 0)
                max = j;
        }

        temp = totalListOfItems.get(i);
        totalListOfItems.set(i, totalListOfItems.get(max));
        totalListOfItems.set(max, temp);
    }
}

Your problem lies here:

if (totalListOfItems.get(max).getValue().compareTo(totalListOfItems.get(j).getValue()) > 0)
  max = j;

here you compare item at position max and j, and if item(max) > item(j), you replace max with j. This is basically searching for LOWEST value, not HIGHEST. Switch it over, and your problem is solved.

Java helps Object Oriented programming, why to implement from scratch when Java collection framework (along with supporting classes) provides ready made proved solutions.

If the objective of your method is just to identify the maximum/ minimum or sort list, then java.util.Collections class provides util methods. only requirement is that your Item class should be (IsA relationship) Comparable, meaning Item should implement Comparable interface. If you do not have control over Item class code, then we can use Interface Comparator to provide comparison rule. the sample code looks as follows.

public static void pickMostExpensiveFirst(ArrayList<Item> totalListOfItems) {
    System.out.println(Collections.max(totalListOfItems));
    // Collections.sort(totalListOfItems); // to sort with Comparable
    // Collections.sort(totalListOfItems, ValueComparator); // to sort with
    //                                                         Comparator
}

class Item implements Comparable<Item> {
    String name;
    int value;

    public Item(String name, int value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public int compareTo(Item other) {
        return Integer.compare(this.value, other.value);
        // return -1 * Integer.compare(this.value, other.value); in case you 
                                                     //need descending order

    }

    @Override
    public String toString() {
        return name + " " + value;
    }

}

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