473,322 Members | 1,421 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,322 software developers and data experts.

I need to alphabetically sort from A to Z, but my code is Z to A

My sort algorithm is sorting from Z to A, I need it to be A to Z PLEASE HELP


Expand|Select|Wrap|Line Numbers
  1. void SortArray (char names[MAXNAMES][LISTLENGTH])
  2.      {
  3.      bool swap;
  4.      int temp[LISTLENGTH];
  5.  
  6.      do
  7.     {
  8.     swap=false;
  9.     for (int i=0; i<MAXNAMES-1; i++)
  10.         {
  11.         if (strcmp(names[i],names[i+1])<0)
  12.             {
  13.             for (int j=0; j<LISTLENGTH; j++)
  14.                 {
  15.                 temp[j]=names[i][j];
  16.                 names[i][j]=names[i+1][j];
  17.                 names[i+1][j]=temp[j];
  18.                 swap=true;
  19.                 }
  20.             }
  21.  
  22.         }
  23.          } while(swap);
  24. }
  25.  
Apr 24 '07 #1
5 1622
RedSon
5,000 Expert 4TB
Just sort Z to A and then reverse your array.
Apr 24 '07 #2
Just sort Z to A and then reverse your array.


Expand|Select|Wrap|Line Numbers
  1.  
  2. void ShowArray (char name[MAXNAMES][LISTLENGTH],int x)
  3. {
  4.     int length;
  5.  
  6.     for(int i= MAXNAMES-x; i<MAXNAMES; i++)
  7.     {length= strlen(name[i]);
  8.     for(int j=0; j<(length); j++)
  9.     {
  10.         cout << name[i][j];
  11.  
  12.     }
  13.     cout << endl;
  14.  
  15.     }
  16. }    
  17.  
How do I reverse this PLEASE?
Apr 24 '07 #3
RedSon
5,000 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1.  
  2. void ShowArray (char name[MAXNAMES][LISTLENGTH],int x)
  3. {
  4.     int length;
  5.  
  6.     for(int i= MAXNAMES-x; i<MAXNAMES; i++)
  7.     {length= strlen(name[i]);
  8.     for(int j=0; j<(length); j++)
  9.     {
  10.         cout << name[i][j];
  11.  
  12.     }
  13.     cout << endl;
  14.  
  15.     }
  16. }    
  17.  
How do I reverse this PLEASE?
Just swap the last element from the first element. Or you can create a new blank array and copy the last element into the first element of the new array. Then lather, rinse, repeat.

Or if your array is implemented in a linked list you can just swap the head for the tail.
Apr 24 '07 #4
Ganon11
3,652 Expert 2GB
I think you can alter your code slightly to get the correct answer. You are testing to see if strcmp(firstString, secString) > 0 - instead, test if strcmp(firstString, secString) < 0.
Apr 25 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
This code in your sort:
(strcmp(names[i],names[i+1])<0)
Needs to vary based on what you desired osrt sequence is. You can't be changing the sort code every time you need a differsent sequence.

This coomon problem is handled by passing in a function pointer as the third argument. This function point will return true if names[i] <names[i+1].

Now you can write all kinds of compare funcitons that return true foe all kinds of reasons. Check the STL sort() algorithm.
Apr 26 '07 #6

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

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
7
by: Robert | last post by:
I have a php/mysql query working like so: $Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%' " All I want to do now is sort them alphabetically. By using the above...
16
by: jagg | last post by:
Hi, how do I have to change/edit my code that the output is sort by the file name (from a-z)? <?php $nr= 0; $handle=opendir ($path);
3
by: A.T. | last post by:
Am trying to sort the following file with the <xsl:sort> function, but can't get it to work properly. Would like the id column sorted according both alphabetically & numerically. (e.g. A-01, A-02,...
0
by: Brent | last post by:
If my field has something like this: 1. dez 2. eft 3. 4. hgt 5. Is there any way to put the non-blank fields on top, sorted alphabetically A-Z, and the blank fields on bottom in...
2
by: rufpirat | last post by:
Hello I'm in the middle of trying to build an "AD phone book", and this being my first try at asp.net, I have a few questions that I hope some of you might be able to help with: 1. Is it...
1
by: k1ckthem1dget | last post by:
My program displays the unsorted list of names in the array. How do i display these names in alphabetical order also. Here is my code. #include <iostream> using namespace std; const int...
10
by: Frank | last post by:
I have a text file, one word per line. I want to arrange the lines alphabetically so I opened it into MS Word and asked Word to sort it. Word said that the list was too big for it. I figure I'll...
3
by: CoreyWhite | last post by:
This code runs @ http://www.ckoogle.com but the trouble is once I get the list backwards, and in an array. I can't sort the new array into alphabetical order. Anyone know a way to do it? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.