Link to home
Start Free TrialLog in
Avatar of unistudent
unistudent

asked on

Java Collections sort by name - syntax error

Hi experts,

I have a problem using List, Collections.sort() <== got a syntax error

I have a List of data like below:

         SchoolContactInfo schoolContactInfo [] = {
                  new ArtStudent("Paul", "987654561"),
                  new ArtStudent("Avie", "987654561"),
                  new MathsStudent("John","98769832")};

         List<SchoolContactInfo > mylist = new ArrayList<SchoolContactInfo >();
         mylist.add(schoolContactInfo [0]);
         mylist.add(schoolContactInfo [1]);
         mylist.add(schoolContactInfo [2]);

                    Collections.sort(mylist);

What I want the output sorted by name using Collections sort but i got sysntax saying

SchoolContactInfo is not valid substitute of bounded parameters

My other class like below:

public abstract class SchoolContactInfo {
      private String name;
      private String phone;

      // Constructor
      public ContactInfo(String Name, String Phone) {
            name = Name;
            phone = Phone;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getPhone() {
            return phone;
      }

      public void setPhone(String phone) {
            this.phone = phone;
      }
}

public class ArtStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

public class MathsStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

===
please correct my syntax. notes i want the output of student sorted by the name
Avatar of for_yan
for_yan
Flag of United States of America image

You class
SchoolContactInfo

should implement Comparabale interface
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You also hneed to keep in mind that if you define constructor - it should have the same name as the class:

public class ArtStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

Open in new window


this acuses erroer as you "contructor" has name FriendA ()
different form the name of the class ArtStudent - this causes compilatuion error - i corrected this in the code above
Avatar of unistudent
unistudent

ASKER

so clear. excellent. i am glad he helps me.