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
You can use this as well, instead of using Array.Sort.
names = names.OrderBy(p => p.Name).ToArray();
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.
This shall do the trick
Array.Sort(names, (x,y) => String.Compare(x.Name, y.Name));
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();
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)
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();