Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends i am trying to access a file with some one lakh data i want to sort the values in the file(file contains integers) how can i do it? pls help me out...
Posted

Read the whole data to array or list. Sort the array or list. Overwrite the file.
 
Share this answer
 
Comments
project.gearup 17-Sep-10 3:27am    
thats a very abstract answer. i cant read one lakh values to a array memory overflow problem will occur.
Baji Jabbar 17-Sep-10 3:43am    
Ya .Thats correct . I didnt thought so . Any way need to hold the data , Is it possible to use a DataTable and sort it.
Baji Jabbar already gave you the right answer, however it looks like you are still in trouble. What are your doubts about?
Are you able to read the file and fill the array (you may need to dynamicalli (re)allocate memory to fit all the integers into the array, see realloc at MSDN[^])?
Are you able to sort the array (you may use qsort[^] for the purpose)?

If C++ usage is an option then your task would be easier.
:)
 
Share this answer
 
Exactly how you do this depends on the nature of your data and how it's stored. So...

- if your data will fit into memory read the entire file, sort it and write it back

- if your data won't fit into memory but will fit into the address space of your process and is in binary form of the correct endianess for your processor open a memory mapped file and sort that

- if your data is in text mode then you'll have to cook it a bit first. Consider reading each integer and enter them into a frequency table and/or hash them depending on the distribution of your data

If your data gets really big then start considering using some database style techniques like partioning the data as you read it in, sorting those partitions and writing them back out again. You might have to consider resizing the partitions if it turns out the one you're processing won't fit into memory. This requires multiple passes over the source file though.

Cheers,

Ash
 
Share this answer
 
You need to read the values from the file - then sort them - then update the file. For example if the values in the file and separted by a comma - you can do this:


System.IO.StreamReader reader = new System.IO.StreamReader("your file path");
string allValues = reader.ReadToEnd();
reader.Close()

string[] values = allValues.Split(',');

List<int> intValues = new List<int>();

foreach (string val in values)
{
     int intVal = int.Parse(val.Trim());
     intValues.Add(intVal);
}

intValues.Sort();

string newAllValues = "";
foreach (int intVal in intValues)
{
    newAllValues += intVal.ToString() + ",";
}

System.IO.StreamWriter writer= new System.IO.StreamWriter ("your file path");
writer.Write(newAllValues);
writer.Close();
 
Share this answer
 
Comments
CPallini 17-Sep-10 3:37am    
Did you see the 'C' tag?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900