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
Java

Avatar of undefined
Last Comment
unistudent

8/22/2022 - Mon
for_yan

You class
SchoolContactInfo

should implement Comparabale interface
ASKER CERTIFIED SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
for_yan

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
unistudent

ASKER
so clear. excellent. i am glad he helps me.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck