Sorting an array alphabetically in C# -
hope can help. have created variable length array accept several name inputs. want sort array in alphabetical order , return console screen.
i thought array.sort(names); me getting exception thrown. have been looking @ notes, examples , on-line nothing seems match doing.
i have done below far. close tearing hair out here! ps have been trying figure out hours , 30+ years old trying learn myself, please don't "do homework" have tried resolve , can not need explain going wrong. sunday , trying work , have no notes cover 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 there?"); input = console.readline(); numberofstudents = int.parse(input); names = new student[numberofstudents]; (int = 0; < 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);*** (int = 0; < names.length; i++) { console.writeline(names[i].name); } } } }
this shall trick
array.sort(names, (x,y) => string.compare(x.name, y.name));
Comments
Post a Comment