Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

Problem sorting object array

  • Please log in to reply




  • Member
  • PipPip
  • 33 posts
In bold is my program.

using System;
using System.Collections.Generic;
using System.Text;

namespace FriendList.cs
class MainClass
public static void Main()
int counter = 0; // Counter for the array
Friend[] friendArray = new Friend[8]; // Single array for all eight friends
string sequence; // Variable to help tell the user which friend they are entering data for

while (counter < 8) // Loop that stops after the eighth entry
friendArray[counter] = new Friend();

// If statements that aid in displaying the sequence of friends
if (counter == 0)
sequence = "st";
else if (counter == 1)
sequence = "nd";
else if (counter == 2)
sequence = "rd";
sequence = "th";

// The lines below accept user input into the array
Console.WriteLine("What is your {0}{1} friend's first name?", counter + 1, sequence);
friendArray[counter].name = Console.ReadLine();
Console.WriteLine("What is your friend's phone number? Ex: 5551234567");
friendArray[counter].phoneNumber = Console.ReadLine();
Console.WriteLine("What month was your friend born in?");
friendArray[counter].birthMonth = Console.ReadLine();
Console.WriteLine("What date of that month was your friend born on?");
friendArray[counter].birthDay = Console.ReadLine();
Console.WriteLine("What year was your friend born in?");
friendArray[counter].birthYear = Console.ReadLine();

counter++; // Adds 1 to the counter so that it accurately tells what friend is currently shown

/*for (counter = 0; counter < 7; counter++)
int swapVariable = 0;
swapVariable = String.Compare(Friend.name[counter], Friend.name[counter + 1]);
if (swapVariable == 1)
Friend.Swap(Friend.name[counter], Friend.name[counter + 1]);

for(counter = 0; counter < 8; counter++)
Console.WriteLine("{0}'s phone number is {1}, and {0}'s birthday is {2} {3}, {4}.", friendArray[counter].name, friendArray[counter].phoneNumber, friendArray[counter].birthMonth, friendArray[counter].birthDay, friendArray[counter].birthYear);
class Friend : IComparable
public string name; // String that contains the name of the friend in each spot of the array
public string phoneNumber; // String that contains the phone number of the friends in the array
public string birthMonth; // String that contains the birth month of the friends in the array
public string birthDay; // String that contains the birthday of the friends in the array
public string birthYear; // String that contains the birth year of the friends in the array

// Functions set up to take in different categories of info for friends
public string GetName()
return name;
public string GetPhoneNumber()
return phoneNumber;
public string GetBirthMonth()
return birthMonth;
public string GetBirthDay()
return birthDay;
public string GetBirthYear()
return birthYear;

int IComparable.CompareTo(Object o)
int returnVal;
Friend temp = (Friend)o;
returnVal = String.Compare(this.name, temp.name);

return returnVal;

I italicized a part of the program I started but I'm unsure as to whether I need it or not, so I commented it out. I need to alphabetize the array by the names of the Friend object array. Based on examples in my book, I wrote the IComparable function, but I can't figure out what I have to do with the value it returns. I probably need a loop and an if statement, right? But if so, where do I put them?
  • 0


Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP