473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sorting items within a file

hay...i want to know how i can sort the records within text file...using c
Mar 26 '07 #1
5 2585
sicarie
4,677 Expert Mod 4TB
Here are our Posting Guidelines.

There are many ways to do that, and many different things that need clarification on that - for instance, are you re-ordering the file, or just printing stuff out? Do you want this in a data structure so you can manipulate it (this is probably necessary either way...)? Are you concerned with time - do you want the best algorithm? Is this a text file, numbered file - how do you want them sorted (numerically/alphabetically) and what is being sorted?

Please post another, more specific question about an exact difficulty you are having, as well as what you have tried to fix it.
Mar 26 '07 #2
i hve to sort records.....within the file..its text file....and sort them acccording to time feild ...
Mar 26 '07 #3
sicarie
4,677 Expert Mod 4TB
i hve to sort records.....within the file..its text file....and sort them acccording to time feild ...
Ok, what have you tried? What have you searched? Can you post the code and any error messages you have gotten?
Mar 26 '07 #4
i read the file record wise..then,, i stored the records within a linked list..sorted the list...wrote it back to file....but i wana know some other way..
i wana know how to sort the within the file
Mar 26 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Sorting within the file itself requires a flat file. That is, each record in the file is the same length. That means no compression. Given that, you can use the file itself as the linked list. Records are fetched and writtten using seek() and tell().

Now all you need is a sort algorithm.

Here's two:

Easiest and slowest is the Exchange Sort (also called Bubble Sort):

1) start with n = 0 and compare record[n] to record[n+1] using your sort keys
2) if record[n+1] < record[n], then swap record[n] with with record[n+1].
3) increment n and repeat steps 1 and 2 until you get to the end of the file
4) when there are no swaps in steps 1-3, the file is sorted.

Trial Minimum:
1) start with n= 0 and call that record[n] the trial minimum
2) compare all other records in the file to this trial minimum. If any record is less, then swap that record with record[n].
3) when you get to the end of the file, the minimum for the file is record[n].
4) Increment n and repeat steps 1-3.
5) When the starting record[n] is the last record in the file, the file is sorted.

Both of these methods require only the key portions of the records to be read into memory. Clever designers precede the record with a "sort handle" that contains all of the fields you are allowed to sort by. In the sort only the sort handle need be read into memory.
Mar 26 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Lad | last post by:
Hi, I have a file of records of 4 fields each. Each field is separated by a semicolon. That is Filed1;Ffield2;Field3;Field4 But there may be also empty records such as ;;;; (only...
3
by: Philipp Lenssen | last post by:
I have this XML file which I would like to XSLT-sort based on the string contained within the item-children elements using basic MSXML (no recent version of IIS, so it might be an outdated MSXML --...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
3
by: Curtis | last post by:
I am trying to do a simple sort on a list view.With the listview control the first time I sort on the control it sorts the items but anytime after that it fails to sort. I'm sure this is a simple...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
1
by: jasydnor | last post by:
Hi, New to the forums and have a question. I've only been developing for about five months now so I apologize if this seems oversimplistic....I am writing a program on an idea I had on a whim. ...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.