Sush
Sush

Reputation: 317

Need to List in sorted according to name in java

I need to sort list according to name but I am able to do that.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaexception;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Admin
 */
class person
{
    int id;
    String name;
};

public class JavaException 
{
    public static void main(String a[])
    {        
        List<person> li =new ArrayList<person>();
        person p=new person();
        p.id=1;
        p.name="Sushant";
        li.add(p);
        person p1=new person();
        p1.id=2;
        p1.name="Atul";
        li.add(p1);
        person p2=new person();
        p2.id=3;
        p2.name="Genu";
        li.add(p2);
        System.out.println(""+li);
        Collections.sort(li);
        for(int i=0;i<li.size();i++)
        {
            person pp=(person)li.get(i);
            System.out.println(""+pp.name);
        }
    }
}

it gaves me an error

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Collections.sort
[javaexception.person@14b7453, javaexception.person@c21495, javaexception.person@1d5550d]
    at javaexception.JavaException.main(JavaException.java:41)

Upvotes: 1

Views: 298

Answers (3)

SMA
SMA

Reputation: 37023

As per the doc for sort method with just list as parameter, it says:

Sorts the specified list into ascending order, according to the
Comparable natural ordering of its elements.
All elements in the list must implement the Comparable
interface.  Furthermore, all elements in the list must be
mutually comparable (that is, e1.compareTo(e2)
must not throw a ClassCastException for any elements
e1 and e2 in the list).

So your person class itself is not comparable hence you would resolve this in two ways:

  • Either implement Comparable interface for your person class and implement compareTo method. Something like:

    class person implements Comparable<person>
    {
    int id;
    String name;
    @Override
    public int compareTo(person o) {
      return this.name.compareTo(o.name);
    }
    };
    
  • use another sort api which takes comparator as argument something like:

    Collections.sort(li, new Comparator<person>() {
    @Override
    public int compare(person o1, person o2) {
    return o1.name.compareTo(o2.name);
    }});
    

Upvotes: 2

Ouney
Ouney

Reputation: 1194

Whenever you call Collections.sort() on a list of object. Then java does not know which field to sort it on. In your case you have id and name. How will java infer whether you want to sort on name or id. So, you need to mention the criteria for sorting.

To do that you can do following : -

Make your person class extends Comparable

class person implements Comparable

and then implement compareTo method. So, when you will call Collections.sort() java will call person.compareTo to compare and sort the objects.

Another way to is to use comparator

http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

Upvotes: 1

Alexis C.
Alexis C.

Reputation: 93842

When using Collections.sort(List<T> list), the compiler requires that the type T must be comparable (<T extends Comparable<? super T>>).

This is not the case for your Person class. Either make the Person class be comparable (by implementing the Comparable interface), or provide a custom comparator using the overloaded sort method.

Upvotes: 3

Related Questions