-1

So my ArrayList looks like this:

        using System.Collections;
        [...]
        //in main:
        Student s01 = new Student
        {
            name = "John",
            surname = "Doa"
        };

        Student s02 = new Student
        {
            name = "John",
            surname = "Doe"
        };

        Student s03 = new Student
        {
            name = "John",
            surname = "Doi"
        };

        Student s04 = new Student
        {
            name = "John",
            surname = "Doo"
        };

        Student s05 = new Student
        {
            name = "John",
            surname = "Dou"
        };

        ArrayList studentsList = new ArrayList();
        studentsList.Add(s03);
        studentsList.Add(s04);
        studentsList.Add(s05);
        studentsList.Add(s01);
        studentsList.Add(s02);

Student.cs:

class Student : IPerson
{
    public string name;
    public string surname;

    public void Describe()
    {
        Console.WriteLine("{0} {1}", name, surname);
    }

I'd like to sort my objects in studentsList by surname and write down both name + surname in foreach loop. I know that studentsList.Sort(); won't work, because it doesn't know in what way it should compare each object to other - and that's where I'm stuck, because I don't really know how to write new comparer that's using value of field of object in arrayList. I don't even know how to start.

I have to use ArrayList, but I don't have to sort it directly (I can use casting I guess) - in the end it must look like:

1. John Doa
2. John Doe
3. John Doi
4. John Doo
5. John Dou

Would be great to Sort() studentsList, because I'd be able to do something like this:

int i = 0;
foreach (Student s in studentsList)
{
   i++;
   Console.Write("{0}. ", i);
   s.Describe();
}
8
  • 3
    why must you use ArrayList? is this homework? Commented Jan 25, 2017 at 20:23
  • Kind of. It's a task for a test and I study for it. Commented Jan 25, 2017 at 20:26
  • stackoverflow.com/questions/2202185/… Commented Jan 25, 2017 at 20:29
  • 1
    You will also need to add using System.Linq for it to work. Commented Jan 25, 2017 at 20:33
  • 2
    Looks like you missed the OfType<Student>() call. But please be aware that ArrayList is very rarely used in real code these days - it's been mostly-obsolete since 2005. Commented Jan 25, 2017 at 20:34

3 Answers 3

1

You can also define a compareTo method in Student to be able to use studentList.Sort() . Student class should also extend :IComparable

  public int CompareTo(object obj)
  {
        if (obj == null) return 1;
        Student other = obj as Student;
        if (other != null)
        {
            // Compare last names first, then by firstName
            int value = this.lastName.CompareTo(other.lastName);
            if (value == 0)
                value = this.firstName.CompareTo(other.firstName);
            return value;
        }
        else
            throw new ArgumentException("Object is not a Student");
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, works great - not as simple as linq expression, but .Sort() works ;)
0

You can do it as following

        ArrayList studentsList = new ArrayList();
        studentsList.Add(s03);
        studentsList.Add(s04);
        studentsList.Add(s05);
        studentsList.Add(s01);
        studentsList.Add(s02);

        var studentsList2 = studentsList.Cast<Student>().OrderBy(r => r.surname).ToList();

        int i = 0;
        foreach (Student s in studentsList2)
        {
            i++;
            Console.Write("{0}. ", i);
            s.Describe();
        }

2 Comments

That statement won't achieve anything - you'd need to use the result of the call.
Works too, but the one that suggested @Will is better, because I don't have to use indirect sorting (using the list).
0
foreach (var student in studentsList.OfType<Student>().OrderBy(x => x.surname).ThenBy(x => x.name))
        {
            i++;
            Console.Write("{0}. ", i);
            student.Describe();
        }

works as it should (thanks to @Will, https://stackoverflow.com/users/1228/will), I didn't realize that linq expression would be that easy. That being said, it's only academic example because ArrayList is mostly obsolete since 2005 (@JonSkeet).

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.