1 Replies - 2293 Views - Last Post: 04 September 2009 - 06:26 PM Rate Topic: -----

#1 angelvale   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-September 09

Showing data from other text file after searching or sorting other tex

Post icon  Posted 04 September 2009 - 05:19 PM

Hello. How will I show the other data that came from the other text file after I sort and search a different text file?

For example, I searched for the name which is saved from another text file, how will i show the corresponding data which are saved from different text files for the name i searched?

For example, for searching:


Search: Lopez
Last Name	   First Name	  Age	   Gender
Lopez		   John			18		Male




for sorting:

before:

Sorting by Last Name:
Lopez		 John
Belasco	   Kelly
Santiago	  Kristine
Feroia		Peter




after:

Belasco	  Kelly
Feroia	   Peter
Lopez		John
Santiago	 Kristine





How will i be able to do that?

Is This A Good Question/Topic? 0
  • +

Replies To: Showing data from other text file after searching or sorting other tex

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Showing data from other text file after searching or sorting other tex

Posted 04 September 2009 - 06:26 PM

1. Load text file data into a collection.
2. Search the collection for whatever you want.
3. Sort the collection.
4. Write sorted collection out to another text file.


Which part are you having trouble with?

Here's some demo code i hacked together:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SortNSearchDemo
{
	class Program
	{
		private static List<Person> people = new List<Person>();

		static void Main(string[] args)
		{
			people = GetPeople();

			// do some stuff with it. you wanted to search?
			Person lopez;
			bool findResult = FindPersonByLastName("Lopez", out lopez);
			
			// sort on last name
			people.Sort();

			// Output sorted results to file.
			SaveSortedPeople();
		}

		private static List<Person> GetPeople()
		{
			using (StreamReader reader = new StreamReader(Path.Combine(Environment.CurrentDirectory, "data.txt")))
			{
				// ditch the first line. it's a header
				reader.ReadLine();

				string line = String.Empty;
				while ((line = reader.ReadLine()) != null)
				{
					string[] parts = line.Split(' ','\t');
					List<string> cleanParts = new List<string>();

					foreach (string part in parts)
					{
						if (part != String.Empty)
						{
							cleanParts.Add(part);
						}
					}

					people.Add(new Person()
					{
						LastName = cleanParts[0],
						FirstName = cleanParts[1],
						Age = cleanParts[2],
						Gender = cleanParts[3]
					});
				}
			}
			return people;
		}

		private static void SaveSortedPeople()
		{
			using (StreamWriter writer = new StreamWriter(Path.Combine(Environment.CurrentDirectory, "sortedData.txt")))
			{
				foreach (Person person in people)
				{
					writer.WriteLine("{0,-20}{1,-20}{2,-5}{3,-10}", person.LastName, person.FirstName, person.Age, person.Gender);
				}
			}
		}

		private static bool FindPersonByLastName(string lastName, out Person foundPerson)
		{
			try
			{
				foundPerson = people.Where(x => x.LastName == lastName).Single();
			}
			catch (InvalidOperationException)
			{
				foundPerson = null;
				return false;
			}

			return true;
		}

	}

	class Person : IComparable
	{
		public string FirstName { get; set; }
		public string LastName { get; set; }
		public string Age { get; set; }
		public string Gender { get; set; }

		public int CompareTo(object obj)
		{
			Person p = (Person)obj;
			return this.LastName.CompareTo(p.LastName);
		}
	}
}



Here was my sample:

Last Name		First Name		Age			Gender
Lopez			John			18			Male
Belasco			Kelly			20			Female
Santiago		Kristine		21			Female
Feroia			Peter			22			Male



Here's the sorted out:

Belasco			 Kelly			   20   Female	
Feroia			  Peter			   22   Male	  
Lopez			   John				18   Male	  
Santiago			Kristine			21   Female   



Hopefully this helps get you started.

Edit: I'm pretty new here and I realized after some time of posting this that I probably broke the rule where the poster actually does *some* work in regards to the problem.

I would like to say to original poster: If you just decide to directly hand in my code as your own, be prepared to do a lot of explaining to your instructor because they are going to know right off the bat that you did not code it yourself.

I posted a full albeit crappy solution to your problem, but it's a launch pad toward further understanding of your own project. Don't skip the foot work.

This post has been edited by MentalFloss: 05 September 2009 - 05:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1