23

Hope someone can help. I have created a variable length array that will accept several name inputs. I now want to sort the array in alphabetical order and return that to the console screen.

I thought that Array.Sort(names); would do this for me but I am getting an exception thrown. I have been looking at notes, examples and on-line but nothing seems to match what I am doing.

I have done the below so far. I am close to tearing my hair out here! PS I have been trying to figure this out for hours and I am 30+ years old trying to learn myself, so please don't just say "Do your homework" I have tried to resolve this and can not so I need someone to explain where I am going wrong. It is a Sunday and I am trying to do extra work and have no notes to cover this exactly

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Student_Array
    {
        class Program
        {
            struct Student
            {
                public string Name;
            }
    
            static void Main(string[] args)
            {
                int numberOfStudents;
                Student[] names;
                string input;
    
                Console.WriteLine("How many students are there?");
                input = Console.ReadLine();
                numberOfStudents = int.Parse(input);
    
                names = new Student[numberOfStudents];
                
    
                for (int i = 0; i < names.Length; i++)
                {
                    Student s;
                    Console.WriteLine("Please enter student {0}'s name", (i + 1));
                    s.Name = Console.ReadLine();
                    names[i] = s;
                }
                ***Array.Sort<Student>(names);***
                for (int i = 0; i < names.Length; i++)
                {
                   
                    Console.WriteLine(names[i].Name);
                }
            }
        }
    }
Nexo
  • 2,125
  • 2
  • 10
  • 20
user001
  • 415
  • 1
  • 7
  • 22

7 Answers7

44

This shall do the trick

Array.Sort(names, (x,y) => String.Compare(x.Name, y.Name));
oleksii
  • 35,458
  • 16
  • 93
  • 163
7

Your issue here might be that you are confusing the notions of students and names. By defining the Student struct, you are creating an entity that can represent more than a mere name. You could, for example, extend it to include Age, Hometown, and so forth. (For this reason, it might be more meaningful to name your array students rather than names.)

struct Student
{
    public string Name;
    public int Age;
    public string Hometown;
}

Given the possibility of multiple fields, the Array.Sort method needs to know what you want to sort your list upon. Do you want students ordered by name, by age, or by hometown?

Per the MSDN documentation on Array.Sort<T>:

Sorts the elements in an entire Array using the IComparable<T> generic interface implementation of each element of the Array.

This means that the type you are attempting to sort – in your case, Student – must implement the IComparable<T> interface, in order for the Array.Sort implementation to know how it should compare two Student instances. If you're convinced that students will always be sorted by name, you could implement it like so:

struct Student : IComparable<Student>
{
    public string Name;
    public int Age;
    public string Hometown;

    public int CompareTo(Student other)
    {
        return String.Compare(this.Name, other.Name);
    }
}

Alternatively, you could provide a function that extracts the sort key to the sort method itself. The easiest way of achieving this is through the LINQ OrderBy method:

names = names.OrderBy(s => s.Name).ToArray();
Douglas
  • 53,759
  • 13
  • 140
  • 188
5

You can either use Sort as is if you extend Student to implement IComparable;

    struct Student : IComparable<Student>
    {
        public string Name;
        public int CompareTo(Student other)
        {
            return String.Compare(Name, other.Name,
                   StringComparison.CurrentCultureIgnoreCase);
        }
    }

...or you can pass a compare lambda into Sort...

Array.Sort<Student>(names, (x, y) => String.Compare(x.Name, y.Name,
                                     StringComparison.CurrentCultureIgnoreCase));

...or as a third option just create a new, sorted, array;

var newArray = names.OrderBy(x => x.Name.ToLower()).ToArray();
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
1

You can use this as well, instead of using Array.Sort.

names = names.OrderBy(p => p.Name).ToArray();
cvraman
  • 1,687
  • 1
  • 12
  • 18
1

Create a comparer class

class StudentComparer : IComparer<Student>
{
    public int Compare(Student a, Student b)
    {
        return a.Name.CompareTo(b.Name);
    }
}

Sort:

Array.Sort(students,new StudentComparer());
Rezo Megrelidze
  • 2,877
  • 1
  • 26
  • 29
0

To sort by the name property of your Student objects in your Student array, you can use

Array.Sort(names, (s1, s2) => String.Compare(s1.Name, s2.Name));

which will sort your array in place, or with System.Linq:

names = names.OrderBy(s => s.Name).ToArray();

which can return the sorted IEnumerable as an array (.ToArray()) or a list (.ToList().)

Remember to sort case-insensitive if it matters, as pointed out in another answer, which can be done in String.Compare like so:

String.Compare(s1.Name, s2.Name, StringComparison.CurrentCultureIgnoreCase)
Elle
  • 3,695
  • 1
  • 17
  • 31
-4

you can find one of the basics algorithms here : Simple bubble sort c#

you have to do some modifications , that example is for int, for string you must compare the names.

you can find better algorithms for sorting. for now bubble sort is ok for you.

Community
  • 1
  • 1
  • 3
    -1 for not giving an answer, but refering to other links or some phrases which are, for the OP, not a concrete answer. – Martin Mulder May 19 '13 at 16:59