2

I have a Fruit model class:

public class Fruit{
   private String name;
   private String color;

   public Fruit(String name, color){
     this.name = name;
     this.color = color;
   }
   public getName(){
      return name;
   }
   public getColor(){
      return color;
   }
}

Then, I created a list of fruits:

List<Fruit> fruits = new ArrayList<Fruit>();

fruits.add(new Fruit("Orange","orange"));
fruits.add(new Fruit("Strawberry","red"));
fruits.add(new Fruit("Apple","green"));
fruits.add(new Fruit("Banana","yellow"));

Now, I would like to sort the fruits elements in alphabetical order in terms of fruit name . How can I efficiently sort the fruits list?

6 Answers 6

3

You can use Collections.sort(), defining a Comparator. For example:

Collections.sort(
    fruits,
    new Comparator<Fruit>()
    {
        public int compare(Fruit f1, Fruit f2)
        {
            // Ignore case for alphabetic ordering.
            return f1.getName().compareToIgnoreCase(f2.getName());
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

2

use the following

Collections.sort(fruits);

public class Fruit implements Comparable {

    public int compareTo(Object o) {            
        Fruit f = (Fruit) o;
        return this.getName().compareTo(f.getName());
    }

}

5 Comments

He needs to implement Comparable too (or supply a custom Comparator).
I would like to compare in terms of fruit.name, that's sort the list in name's alphabetical order. how should it be compared?
You should use "... implements Comparable<Fruit>", otherwise, it is fine
Is it really necessary to write Comparable<Fruit>? I have worked without using the class name. But I have change the compareTo() method slightly. If after modification of that, it's still not working, then please add the Comparable<Fruit>. Thanks
Although implementing Compararable works fine, I'd prefer the alternative solution of defining a Comparator (explained in the next answer). The idea of the Comparable interface is to define some kind of "natural order", and alphabetical order is not really natural for Fruits. However, it's all a matter of taste.
1

Implement the comparable interface on the fruit class, and use Collections.sort(list) http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html

Comments

0
public Comparator<Fruit> fruitComparator = new Comparator<Fruit>() {
        @Override
        public int compare(Fruit f1, Fruit f2) {

            return f1.getName().compareTo(f2.getName());
        }
    };

and then:

Collections.sort(fruits, fruitComparator );

Comments

0
public class Fruit implements Comparable<Fruit> {
{
   public int compareTo(Fruit f) {
       if (this.Name == f.Name)
           return 0;
       else if (this.Name > f.Name)
           return 1;
       else
           return -1;
    }
}

Use it like this:

Collections.sort(fruits);

1 Comment

I'm not sure it's a great idea for fruits to have a forced natural ordering.
0

You can use a comparator. For documentation take a look at http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

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.