Jump to content

  • Log In with Google      Sign In   
  • Create Account


Ordering strings


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 28 April 2013 - 03:15 PM

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[] grade = 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
			firstNames[i] = readFirstName(studentArray[i]);
			lastNames[i] = readLastName(studentArray[i]);
			grade[i] = readGrade(studentArray[i]);
			age[i] = readAge(studentArray[i]);
			email[i] = readEmail(studentArray[i]);
		}
		//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



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8478

Like
0Likes
Like

Posted 28 April 2013 - 04:35 PM

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 29 April 2013 - 06:15 AM

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?



#4 Bacterius   Crossbones+   -  Reputation: 8478

Like
0Likes
Like

Posted 29 April 2013 - 06:42 AM

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).


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 29 April 2013 - 07:13 AM

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



#6 dmatter   Crossbones+   -  Reputation: 3027

Like
2Likes
Like

Posted 29 April 2013 - 07:23 AM

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.

 

See the Javadoc for Comparator.

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



#7 Bacterius   Crossbones+   -  Reputation: 8478

Like
0Likes
Like

Posted 29 April 2013 - 07:38 AM

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

 

Ah, thanks for the clarification blink.png 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!


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 29 April 2013 - 12:28 PM

Thanks for the java collections link! It worked :D

 

Thanks for the help :D






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS