# Ordering strings

This topic is 1723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a method which loads data from a Student object. I would like to be able to get a string from that method which holds the data the student class contains.

	public static String getData(String order) throws IOException{

//create string array for each type of data student object can hold
String[] firstNames = new String[numberOfStudents];
String[] lastNames = new String[numberOfStudents];
String[] age = new String[numberOfStudents];
String[] email = new String[numberOfStudents];

for(int i = 0; i < numberOfStudents; i++){
//get data from each student object
}
//HERE i would like to be able to order by first name or last name but i                //would like to keep the last name and grades with its own first name
if(order == "FIRST_NAME"){

}else{

}
return "NULL";
}


I hope I am being clear for what I am trying to achieve. It may be difficult to understand from just looking  at this method.

Thanks

##### Share on other sites

In this case you may be better off creating a Student class which contains the information for a single student, and then have an array of Students, instead of five disjoint arrays each containing some information about each student. Then you should be able to sort the Student array with a custom comparator (based on the first or last name, or anything else about each student), though I am not sure how to do this in C#. This will keep all the data together.

##### Share on other sites

That is exactly what I am doing. I have an array of 20 students. Each read method takes a student object as a parameter and reads the appropriate information from a file for that student. How would I implement such a custom comparator?

##### Share on other sites

That is exactly what I am doing. I have an array of 20 students. Each read method takes a student object as a parameter and reads the appropriate information from a file for that student. How would I implement such a custom comparator?

I don't see an array of students. I see five arrays of strings each containing a piece of data for each student. So my idea was to ditch those string arrays (they are not helping you) and sort the studentArray according to whatever sort order you need. For comparing using a custom function, have you tried this?

http://support.microsoft.com/kb/320727

The idea is to provide some comparison function which will compare two arbitrary Student objects (in there you can decide whether to sort by first name, last name, etc..) and then feed this function to your standard library's sort function (actually, it should work directly if you implement the comparator properly, at least that's how it works in Java). Either way, this should sort the Students correctly and keep their data together (i.e. two students won't end up with wrong ages after sorting by name).

##### Share on other sites

The array of students is created elsewhere and each student contains firstname, lastname, grade, age, email. I would like to list by first name but then have the appropriate lastname, grade etc with that first name. So if i have student[0] and it contains 'b' for every field and i have student[1] and it contains 'a' for every field i would like to have output:

a a a a a

b b b b b

##### Share on other sites

In this case you may be better off creating a Student class which contains the information for a single student, and then have an array of Students, instead of five disjoint arrays each containing some information about each student. Then you should be able to sort the Student array with a custom comparator (based on the first or last name, or anything else about each student), though I am not sure how to do this in C#. This will keep all the data together.

@Bacterius: This is actually Java, not C#.
@nitishk: The advice offered by Bacterius to implement a Student class (rather than using an array for each property) along with a comparator is the way you want to go with this.

So you'll have a List<Student> and a StudentComparator and you can use Collections.sort to reorder the list according to the comparator.

##### Share on other sites

@Bacterius: This is actually Java, not C#.

Ah, thanks for the clarification  I don't know why I assumed it was C#. Though I think the only distinguisher here is the "throws IOException" and the capitalized String type. Sorry!