Hello, I am currently making a program that sorts user input of age and name. If a user inputs their name and age, all out of order, how would I go about sorting this from oldest to youngest or youngest to oldest? I have this so far:

``````        Ages[] ages = new Ages[10];

for (int counter = 0; counter < 10; counter++)
{
string tempName;
int tempAge;

ages[counter] = new Ages(tempName, tempAge);

}

// Sort array.
Array.Sort(ages);

for (int counter = 0; counter < 10; counter++)
{
Console.WriteLine(ages[counter].ToString());
}
}

public class Ages
{

public string initials { get; set; }

public int age { get; set; }

public Age(string pInitials, int pAge)
{
initials = pInitials;
age = pAge;

}

public override string ToString()
{
return string.Format("{0}     {1}", score, initials);
}
}
}
}
``````

Here's a simple Icomparable implementation for your class:

``````    public class Ages:IComparable
{
public string initials { get; set; }
public int age { get; set; }
public Ages(string pInitials, int pAge)
{
initials = pInitials;
age = pAge;
}
public override string ToString()
{
return string.Format("{0} …``````

A note on reversing the sortorder. since the comparer returns one of 3 ints(-1,0,1), to indicate what order the 2 objects should be ordered, simply multiplying by -1 will reverse the 2 outputs that indicate less than or greater than.

## All 6 Replies

To sort descending you could first use Array.Sort and then Array.Reverse method Click Here

Since the system doesn't know how to compare Ages, you need to implement the IComparable interface on your ages class.

Alternitivly you could implement some IComparers to pass to Array.Sort.

i've tried to implement the IComparable interface on my ages class, but I haven't figured that one out. I've used some examples online(from MSDN, and Google) but nothing yet. I'll continue trying.

Here's a simple Icomparable implementation for your class:

``````    public class Ages:IComparable
{
public string initials { get; set; }
public int age { get; set; }
public Ages(string pInitials, int pAge)
{
initials = pInitials;
age = pAge;
}
public override string ToString()
{
return string.Format("{0} {1}", age, initials);
}
public int CompareTo(object obj)
{
Ages x = (Ages)(obj);
return this.age.CompareTo(x.age);
}
}
``````

Array.Sort(ages) will automatically sort by age.
Array.Reverse(ages) will reverse the sort order.

Here's an implementation that uses a simple comp[arer that allows you to sort by ascending or descending. Which might be more efficient for larger arrays, since you will only use one iteration method:

``````class Program
{
public static SortOrder SortedOrder = SortOrder.Ascending;
static void Main(string[] args)
{
Ages[] ages = new Ages[10];
for (int counter = 0; counter < 10; counter++)
{
string tempName;
int tempAge;
ages[counter] = new Ages(tempName, tempAge);
}
// Sort array.
SortedOrder = SortOrder.Descending;
Array.Sort(ages, CompareAges);
for (int counter = 0; counter < 10; counter++)
{
Console.WriteLine(ages[counter].ToString());
}

}
public static int CompareAges(Ages y, Ages x)
{
return (y.age.CompareTo(x.age)) * (int)SortedOrder;
}
}
enum SortOrder : int
{
Ascending = 1,
Descending = -1
}
public class Ages
{

public string initials { get; set; }
public int age { get; set; }
public Ages(string pInitials, int pAge)
{
initials = pInitials;
age = pAge;
}
public override string ToString()
{
return string.Format("{0} {1}", age, initials);
}

}
``````

A note on reversing the sortorder. since the comparer returns one of 3 ints(-1,0,1), to indicate what order the 2 objects should be ordered, simply multiplying by -1 will reverse the 2 outputs that indicate less than or greater than.

commented: Thanks reminding, completely forgot about it. +14

Thank you tinstaafl, I was really getting confused on the -1(or reverse order). I had code I had posted, but I guess it didn't get through, it looks pretty much like how you first posted, but yours is cleaner. Once again, tyvm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.