111k views
0 votes
Consider a structure names consisting of first (named first) and last (named last) names (unsorted), creating an array of them. Using a LINQ query, determine all people whose first name alphabetically precedes their last name. The returned value should be sorted by first name and then by last name.

User Majix
by
7.4k points

1 Answer

1 vote

Answer:

Code implemented using LINQ Query below

Step-by-step explanation:

using System;

using System.Linq;

namespace NameComparision

{

/// <summary>

/// driver program

/// </summary>

class Program

{

public struct Name

{

public string first { get; set; }

public string last { get; set; }

}

static void Main(string[] args)

{

//Create an array of 10 names

Name[] names = new Name[10];

names[0] = new Name() { first = "Bob", last = "Weller" };

names[1] = new Name() { first = "Will", last = "Smith" };

names[2] = new Name() { first = "Pierre", last = "Aoun" };

names[3] = new Name() { first = "Jim", last = "White" };

names[4] = new Name() { first = "Rafal", last = "Brouzuch" };

names[5] = new Name() { first = "Alex", last = "White" };

names[6] = new Name() { first = "Ann", last = "Situ" };

names[7] = new Name() { first = "Mandy", last = "Ibrahim" };

names[8] = new Name() { first = "Nerida", last = "Aschroft" };

names[9] = new Name() { first = "Alex", last = "Lucas" };

//display the array before filtering

Console.WriteLine("Names before filtering in the array");

foreach (var name in names)

{

Console.WriteLine(name.first + "," + name.last);

}

//filter the array where first name precedes last name and then sort the data by first and then last

var namesPreceeds = names.Where(x => x.first.CompareTo(x.last) <= 0).OrderBy(y => y.first).ThenBy(z => z.last);

//display the filtered data

Console.WriteLine("Names after filtering and order by first name and last name");

foreach (var name in namesPreceeds)

{

Console.WriteLine(name.first + "," + name.last);

}

Console.ReadLine();

}

}

}

User Jacob Jedryszek
by
8.6k points