please tell me algorithm or code for following character sorting (Ascending) problem.

For example Text file contains following data:

001, PSTR, abcdef, h.no.222 abcdefghik lmnopq rstuv, student, A-level
002, MKTO, abcdef, house. 2 abcdefghik lmnopq rstuv, student, rtv
003, LORR, abcdef, h.number.210 abcdefghik lmnopq rstuv, student, O-level
005, ASMP, abcdef, h.#.111 abcdefghik lmnopq rstuv, student, mpr
006, SBIL, abcdef, house.no.234abcdefghik lmnopq rstuv, student, h
007, CEGF, seefwd, h. 120 abcdefghik lmnopq rstuv, student, stvdedstg
008, XMEL, decfab, h.no.333 abcdefghik lmnopq rstuv, student, mstvuv
009, BSTU, abcefd, h.no.333 abcdefghik lmnopq rstuv, student, mstvuv
010, ZJRV, defabc, h.no.333 abcdefghik lmnopq rstuv, student, mstvuv


Data is comma separated in the text file. How can i sort in the ascending order on the basis of data that is after first comma for example "PSTR" here.

please tell me using file handling mechanism.


I have done following:

1. used structure to store a string of Key and int line number:

struct myData
{
std::string Key;
int line;
};

myData d[10];

2. extract first number in Integer Line and Column in all object values.

3. Using nested for loop i have done BUBBLE Sort :( . and inserted the sorted sequence in new file.

4. By using nested while loop i have done following:

external loop opens sorted text file first character that is Id. then walks into internal loop which reads line from old file and compares external Id from internal Id if both same than output is shown in the new file.
but this doesn't works fine. it extracts only first 2 records out of 10.

please tell me what should be done here and tell me what should be done when record has many entries (3000 or more) in text file with comma separation.

please tell me easy way to solve the problem.
waiting for reply

Recommended Answers

All 4 Replies

You've found the easiest way to do it. Read each line into a record, break the record up into fields, append the record to an array of records, and sort the array by your chosen field. If you have a lot of records, use a faster sorting algorithm such as shell sort or std::sort. If the file is huge, you may have to sort it in chunks and then merge the chunks into a result file, making use of hard disk storage to cover for any lack of physical memory. But I doubt you'll need to do that.

IF you're on unix:

sort -t ',' k- 1,1 -o newfile.csv  oldfile.csv

will sort the file the way you want - outside C++.

i have made following programe but it doesn't work properly

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ini{
  char itmna[35];
  int itmcd,qty;
  float prc;
};
class ret
{ ini in;
  int cd[20],qty1[20];
  float prc1[20],tot;
  char itmna1[20][35];
  public:
  void add()
  {char ch='y';
   ofstream q("initial.dat",ios::app);
   do
   {
    cout<<"\nEnter the item code: ";
    cin>>in.itmcd;
    cout<<"\nEnter item name: ";
    gets(in.itmna);
    cout<<"\nEnter the quantity: ";
    cin>>in.qty;
    cout<<"\nEnter it's unit price: ";
    cin>>in.prc;
    q.write((char*)&in,sizeof(in));
    cout<<"\nWant to add more items(y\n): ";
    cin>>ch;
    }while(ch=='y'||ch=='Y');
   clrscr();
   q.close();
  }
  void purch()
  { char ch='y';
    int i1=0,j,f;
    ofstream ou("initial.dat",ios::app);
    ifstream i("initial.dat");
    do
    {
    cout<<"\nEnter the item code: ";
    cin>>cd[i1];
    cout<<"\nEnter the quantity: ";
    cin>>qty1[i1];
    while(i)
    { if(in.itmcd==cd[i1])
      { i.read((char*)&in,sizeof(in));
	strcpy(itmna1[i1],in.itmna);
	prc1[i1]=in.prc;
	if(in.qty<qty1[i1])
	{
	cout<<"\nStock not available:";
	}
	else
	{
	in.prc-=prc1[i1];
	ou.write((char*)&in,sizeof(in));
	}
	break;
      }
    else
    {cout<<"\nNot available!!!!!";
     break;
    }
    cout<<"\nPurchase more item(y\n): ";
    cin>>ch;
    i1++;
    }
    }while(ch=='y'||ch=='Y');
    clrscr();
    gotoxy(4,1);
    cout<<"****BILL****";
    gotoxy(2,2);
    cout<<"Item code:\tItem name:\tM.R.P\tQuantity:\tTotal";
    for(j=0;j<=i1;j++)
    { gotoxy(2,j+3);
      cout<<cd[j]<<"\t\t"<<itmna1[j]<<"\t\t"<<prc1[j]<<"\t"<<qty1[j]<<"\t"<<prc1[j]*qty1[j];
      tot+=(qty1[j]*prc1[j]);
    }
    gotoxy(2,i1+4);
    cout<<"TOTAL:\t\t\t\t\t\t\t\t"<<tot;
    i.close();
    ou.close();
  }
 }s;
void main()
{ clrscr();
  s.add();
  s.purch();
  getch();
}

Use code tags please. That kind of post is a real bitch to format properly for a clearer view.
=3

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.