AsYouWereAPx
AsYouWereAPx

Reputation: 31

sorting an arraylist by the value of an object method

I want to sort out my Arraylist<Book> b on the values returned by the method numPoints() in class Book. show() is a method of class Book, that prints out the code. Everything else works okay, just the method sort() is the problem i think.

public class som {
private static ArrayList<Book> b;

public static void main(String[] args) {


b = new ArrayList<Book>();
...
}
...

public static void sort() {
    Book mon = null;
    int indMax = 0;
    for(int i=0; i<b.size()-1; i++){
        mon = b.get(i);
        indMax = i;
        for(int j=i+1; j<b.size(); j++){
            if(b.get(j).numPoints() > mon.numPoints()){
                mon = b.get(j);
            }
        }
        if(indMax != i){
            b.set(indMax, b.get(i));
            b.set(i, mon);
        }

    }
    for(int s=0; s<b.size(); s++) {
        b.get(j).show();
    }
}

class Book {
...
     public double numPoints() {
      ...
     }
     public void show() {
     ...
     }


...
}

i also tried the following code, but neither of them work properly.

public static void sort() {
    ArrayList<Book> o = new ArrayList<Book>();
    double vMax=0;
    int iMax=0;
    while(b.size()!=0) {
        for(int i = 0; i<b.size(); i++)  {
            if(vMax<b.get(i).numPoints()) {
                vMax=b.get(i).numPoints();
                iMax=i;
            }
        }
        o.add(b.get(iMax));
        b.remove(iMax);
    }

    for(int j=0; j<o.size(); j++) {
        o.get(j).show();
    }

The first one just doesn't work properly, but in the second one terminal prints out :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

can somebody please help me figure out what is wrong?

Upvotes: 0

Views: 67

Answers (2)

ichantz
ichantz

Reputation: 298

For java >= java 8

b.sort((b1,b2)-> b1.numPoints()-b2.numPoints());

For java < java 8

Collections.sort(b,new Comparator<Book>{
            @Override
            public int compare(Book b1, Book b2) {
                return b1.numPoints() - b2.numPoints();
            }
});

Upvotes: 1

cocsackie
cocsackie

Reputation: 56

It seems that you are interested in having working code, not implementing sorting from scratch.

Just use already implemented sort method of list:

ArrayList<Book> list;
list.sort(Comparator.comparing(Book::numPoints));

The sort method accepts Comparator.Comparator's job is to compare objects.

Function Comparator.comparing creates comparators. It accepts one parameter that is a Function. Function's job is to provide substitute for an object that would be compared. so the substitute can be compared instead.

Because you want to compare Books by what Book.numPoints method returns, we need to create a function that for each Book returns result of Book.numPoints method. Because Function is a functional interface we can use method reference (Book::numPoints) to provide the function.

Then you can call show on all of them:

list.forEach(Book::show)

Not only it is simple, but it is easy to modify.

Upvotes: 1

Related Questions