Sorting Array in c# [closed]

string[] iniArray = { "M Facci", "D Thornton", "B Luke", "S Tofani", "T Luke" };
var sortedArray = iniArray.OrderBy(r => r.Split(' ').Last()).ToArray();

or (courtesy @killercam)

var sortedArray = iniArray.OrderBy(r => r.Split().Last()).ToArray();

Assuming there is only First Name and Last Name in a string, also, there exists a Last Name.

To display the resulted array:

foreach (string str in sortedArray)

Output would be:

M Facci
B Luke
T Luke
D Thornton
S Tofani

Leave a Comment