hi i need help to sort a file in terms of CourseCode

i have this file COURSE.DAT
In the file this is the format....
<CourseCode>|<CourseName>|<SubjectArea>|<TeacherID|<Date>|<NoEnrolled>|<Support>
<CourseCode>|<CourseName>|<SubjectArea>|<TeacherID|<Date>|<NoEnrolled>|<Support>

eg of this file(raw)
CS10|Computing|CS|20845|12-12-2008|12|1
MA10|Mathematics|MA|20845|12-12-2008|12|1
PH11|Physics Practical|PH|20845|12-12-2008|12|1
CS11|Computing Practical|CS|20845|12-12-2008|12|1
PH10|Physics|PH|20845|12-12-2008|12|1

i need it to be sorted (the end result should be this
CS10|Computing|CS|20845|12-12-2008|12|1
CS11|Computing Practical|CS|20845|12-12-2008|12|1
PH10|Physics|PH|20845|12-12-2008|12|1
PH11|Physics Practical|PH|20845|12-12-2008|12|1
MA10|Mathematics|MA|20845|12-12-2008|12|1


Can anyone give me the code. i am using Microsoft Visual C++ 6.0
Please note the the number of records can be of any number(need not be 5).

I heard my fren say use strcmp and put it in array. And some of them saying to use bubble sort.
If anyone can give the solution...i will really be thankul to you!

Recommended Answers

All 6 Replies

1. Why you created two different threads for the same problem.
@ moderators : please delete new one.
2. What you have done so far? i don't think any one will provide you source code here.

google for bubblesort and you will get the code you want.

First you need to read in the data. Have some flags ready.

Then you have a string array with data. Now you need to sort it.

There are a couple of ways to sort it now. You can google "sorting
c++" and you will find ways to sort data.

use strcmp() to compare first field in each record and use bubblesort.
or use std::vector, or std::list to keep your data
Smth like this:

struct SubjectRecord
{
   char *CourseCode;
   char *CourseName;
   char *SubjectArea;
   int TeacherID;
   time_t Date; //you can use your own class of time
   int NoEnrolled;
   int Support;
   
   bool operator<(const SubjectRecord &s)
   {
         return strcmp(this->CourseCode, s.CourseCode) < 0;
   }
};
//---------------------------smth----------------
std::list<SubjectRecord *> subjects;
//read data from file into structure and put it to the list
subjects.sort(); //sorting

You can use "map" container from STL which automatically sorts its contents by the key. First split each your lines by "|" token, using tokenizer command of c++, then store each line in the map with course code as key and the full line as value. The map finally contains lines sorted by coursecode.

You can use "map" container from STL which automatically sorts its contents by the key. First split each your lines by "|" token, using tokenizer command of c++, then store each line in the map with course code as key and the full line as value. The map finally contains lines sorted by coursecode.

The problem is that rip_snoopy has three threads on the exact same topic. People have already commented on one of the other threads, but not everyone saw that. That's why you're only supposed to start ONE thread. Starting three just wastes everyone's time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.