Reputation: 415
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);
}
}
}
}
Upvotes: 23
Views: 92645
Reputation: 3060
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());
Upvotes: 1
Reputation: 54887
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 theIComparable<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();
Upvotes: 8
Reputation: 180917
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();
Upvotes: 5
Reputation: 3775
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)
Upvotes: 0
Reputation: 1697
You can use this as well, instead of using Array.Sort.
names = names.OrderBy(p => p.Name).ToArray();
Upvotes: 1
Reputation: 81
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.
Upvotes: -4
Reputation: 35905
This shall do the trick
Array.Sort(names, (x,y) => String.Compare(x.Name, y.Name));
Upvotes: 44