Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your issue here might be that you are confusing the notions of students and names. By defining the <code>Student</code> struct, you are creating an entity that can represent more than a mere name. You could, for example, extend it to include <code>Age</code>, <code>Hometown</code>, and so forth. (For this reason, it might be more meaningful to name your array <code>students</code> rather than <code>names</code>.)</p> <pre><code>struct Student { public string Name; public int Age; public string Hometown; } </code></pre> <p>Given the possibility of multiple fields, the <code>Array.Sort</code> method needs to know what you want to sort your list upon. Do you want students ordered by name, by age, or by hometown?</p> <p>Per the MSDN documentation on <a href="http://msdn.microsoft.com/en-us/library/kwx6zbd4.aspx" rel="nofollow"><code>Array.Sort&lt;T&gt;</code></a>:</p> <blockquote> <p>Sorts the elements in an entire <code>Array</code> using the <code>IComparable&lt;T&gt;</code> generic interface implementation of each element of the Array.</p> </blockquote> <p>This means that the type you are attempting to sort – in your case, <code>Student</code> – must implement the <a href="http://msdn.microsoft.com/en-us/library/4d7sx9hd.aspx" rel="nofollow"><code>IComparable&lt;T&gt;</code></a> interface, in order for the <code>Array.Sort</code> implementation to know how it should compare two <code>Student</code> instances. If you're convinced that students will always be sorted by name, you could implement it like so:</p> <pre><code>struct Student : IComparable&lt;Student&gt; { public string Name; public int Age; public string Hometown; public int CompareTo(Student other) { return String.Compare(this.Name, other.Name); } } </code></pre> <p>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 <code>OrderBy</code> method:</p> <pre><code>names = names.OrderBy(s =&gt; s.Name).ToArray(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload