469,621 Members | 1,824 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

string sort, yet again

5
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  *bunch of code
  3. */
  4. struct album{
  5.        string album;
  6.        string artist[6];
  7.        string songs[21];
  8. }
  9. /* morecode */
  10. int main(){
  11.   album p;
  12. //more code
then i read in a text file that contains all the struct's info but puts it into an array, then a big horrible if statement that coppies the info from the array to the struct

the problem is that in the txt file the songs are listed as follows
1. Track1
2. Track2
3. Not the actual track names
et cetera. on to Track21.

i have to sort based on song name.
i know i need to search each string for the '.'
then check the next character after the space.

(oh yeah, the sort is a function)
something along the lines of
Expand|Select|Wrap|Line Numbers
  1. void ssongssort(string ar[]){
  2.      bool done;
  3.      for(int r,done = 0; !done ; ) {
  4.              for (done = ! ( r = 0 ) ; r < 20 ; r + + ) {
  5.                  string temp1= ar[r].substr(5, 1);// the problem is here
  6.                  string temp2 = ar[r+1].substr(5, 1);// and here
  7.                  temp2.assign(temp2,4,15);
  8.                  if(temp1 > temp2){
  9.                           swap(ar[r],ar[r+1]);
  10.                           done = 0;
  11.                           }
  12.                  }
  13.              }
  14.      }
i've seen a nother way, something like
(imagine the strings as " - Track1")
Expand|Select|Wrap|Line Numbers
  1. string gettrack(string st){
  2.        int t = st.find (" - ",0) + 2;// why would lentgh be 0? 
  3.        while (st[t] == ' ') t++;// the 2 would be 2 characters down.
  4.        return st.substr(t);
  5.        }
  6. //which returns the location of the '-'
however i don't think
Expand|Select|Wrap|Line Numbers
  1. p.albumname[0].find(" - ",0)+2;
would work.
Mar 28 '07 #1
8 1610
Savage
1,764 Expert 1GB
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  *bunch of code
  3. */
  4. struct album{
  5.        string album;
  6.        string artist[6];
  7.        string songs[21];
  8. }
  9. /* morecode */
  10. int main(){
  11.   album p;
  12. //more code
  13. /*then i read in a text file that contains all the struct's info but puts it into an array, then a big horrible if statement that coppies the info from the array to the struct
  14.  
  15. the problem is that in the txt file the songs are listed as follows
  16.  1. Track1
  17.  2. Track2
  18.  3. Not the actual track names
  19. et cetera. on to Track21.
  20.  
  21. i have to sort based on song name.
  22. i know i need to search each string for the '.'
  23.  then check the next character after the space.
  24.  
  25. (oh yeah, the sort is a function)
  26. something along the lines of
  27. void ssongssort(string ar[]){
  28.      bool done;
  29.      for(int r,done = 0; !done ; ) {
  30.              for (done = ! ( r = 0 ) ; r < 20 ; r + + ) {
  31.                  string temp1= ar[r].substr(5, 1);// the problem is here
  32.                  string temp2 = ar[r+1].substr(5, 1);// and here
  33.                  temp2.assign(temp2,4,15);
  34.                  if(temp1 > temp2){
  35.                           swap(ar[r],ar[r+1]);
  36.                           done = 0;
  37.                           }
  38.                  }
  39.              }
  40.      }
  41.  
  42. i've seen a nother way, something like
  43. (imagine the strings as " - Track1")
  44. string gettrack(string st){
  45.        int t = st.find (" - ",0) + 2;// why would lentgh be 0? 
  46.        while (st[t] == ' ') t++;// the 2 would be 2 characters down.
  47.        return st.substr(t);
  48.        }
  49. //which returns the location of the '-'
however i don't think
p.albumname[0].find(" - ",0)+2;
would work.

Hi, pabl0

First when posting please use code tags(Just select your code copied/wriiten

here and click #),it makes code easier to read.

Secound i don't think that your for loops are good designed,Why are you using

double for loop? You could only use one do-while loop,like:

Expand|Select|Wrap|Line Numbers
  1. do{
  2.  
  3.                  string temp1= ar[r].substr(5, 1);
  4.                  string temp2 = ar[r+1].substr(5, 1);
  5.                  temp2.assign(temp2,4,15);
  6.                  if(temp1 > temp2){
  7.                           swap(ar[r],ar[r+1]);
  8.                           r++;
  9.                           done = 0;
  10.                 }else done =1;
  11.  
  12. while(done==0);
Hope this helps.

Savage
Mar 28 '07 #2
pabl0
5
it's just a bubble sort (and i've seen some mighty fine one line bubble sorts),
and besides thats not the problem.
the string search is.
Mar 28 '07 #3
Savage
1,764 Expert 1GB
Just one qeustion(Myabe I haven't understood you well):

Is the txt file with no actual track names one that you put in your arrays or

is that a output txt file
Mar 29 '07 #4
pabl0
5
someting along the lines of

ifstream in ("songlist.dat");
string z;
int i = 1;
while (getline(in, z)) {
ar[i] = z;
cout << setw(3) << i++ << " " << z << "\n";
}
Apr 2 '07 #5
Savage
1,764 Expert 1GB
someting along the lines of

ifstream in ("songlist.dat");
string z;
int i = 1;
while (getline(in, z)) {
ar[i] = z;
cout << setw(3) << i++ << " " << z << "\n";
}
So correct me if I'm wrong:

It's the file inputed.You have writen one song in one line and used getline to
get song name.You also set field width to 3 using setw,and couted i, then
couted z and forced start of new line.

Can you post one line of songlist.dat just to see how is it formated?


Savage
Apr 2 '07 #6
pabl0
5
the .dat file is formattd as follows.

Wolfgang Amadeus Mozart //0th element of array
1. Bassoon Concerto in B flat major, K. 191 //1st
2. Concerto for Harp, Flute and Orchestra, K. 299 //2nd
3. Oboe Concerto in C major, K. 314 //3rd
4. Concerto for Clarinet and Orchestra in A major, K. 622 // 4th
5. Flute Concerto No. 1 in G Major, K. 313 // and so on and so forth...

I'm not sure wether this is written corectly for my purpus:
string temp1= ar[r].substr(5, 1);
string temp2 = ar[r+1].substr(5, 1);
setting the temp values as the 5th character of the string,
then later compares those temp values.
Apr 2 '07 #7
Savage
1,764 Expert 1GB
the .dat file is formattd as follows.

Wolfgang Amadeus Mozart //0th element of array
1. Bassoon Concerto in B flat major, K. 191 //1st
2. Concerto for Harp, Flute and Orchestra, K. 299 //2nd
3. Oboe Concerto in C major, K. 314 //3rd
4. Concerto for Clarinet and Orchestra in A major, K. 622 // 4th
5. Flute Concerto No. 1 in G Major, K. 313 // and so on and so forth...

I'm not sure wether this is written corectly for my purpus:
string temp1= ar[r].substr(5, 1);
string temp2 = ar[r+1].substr(5, 1);
setting the temp values as the 5th character of the string,
then later compares those temp values.
Maybe,you don't even have to use strings,instead use char arrays and compare
first char array with another using same array index.
Apr 2 '07 #8
pabl0
5
if i knew C i'd mess with the char arrays, but i don't know C.
however i finally got it to work, for the most part.
Expand|Select|Wrap|Line Numbers
  1. void SongSort( album b[], int r){
  2.      string str[n];
  3.      for(int j = 0; j < 5; j++){
  4.              for(int m = 0; m < r; m++){
  5.                      if( b[j].songs[m] != ""){
  6.                           string temp88 = b[j].songs[m];
  7.                           temp88 = temp88.substr(4,temp88.length()-1); // starts at the 4th character
  8.                           str[m] = temp88;// couldn't have b[j].songs[m].substr(4,temp88.length()-1)
  9.                           b[j].songs[m] = temp88;
  10.                           }
  11.                      }
  12.              for(int i,done = 0; !done;){
  13.                      for (done = !(i = 0); i < r-1; i++){
  14.                          if (str[i] > str[i+1]){
  15.                                     swap(str[i], str[i+1]);
  16.                                     swap(b[j].songs[i], b[j].songs[i+1]), done = 0;
  17.                                     }
  18.                          }
  19.                      }
  20.              }
  21.      }
Apr 9 '07 #9

Post your reply

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

Similar topics

19 posts views Thread by Espen Ruud Schultz | last post: by
11 posts views Thread by Nobody | last post: by
3 posts views Thread by TituscroW | last post: by
7 posts views Thread by al | last post: by
17 posts views Thread by Chad Myers | last post: by
6 posts views Thread by Don | last post: by
12 posts views Thread by aparnakakkar2003 | last post: by
reply views Thread by JosAH | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.