Click here to Skip to main content
14,388,066 members
Rate this:
Please Sign up or sign in to vote.
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...
Rate this:
Please Sign up or sign in to vote.

Solution 1

Read the whole data to array or list. Sort the array or list. Overwrite the file.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
Rate this:
Please Sign up or sign in to vote.

Solution 4

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.


Rate this:
Please Sign up or sign in to vote.

Solution 2

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();

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

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

foreach (string val in values)
     int intVal = int.Parse(val.Trim());


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

System.IO.StreamWriter writer= new System.IO.StreamWriter ("your file path");
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100