GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Oct-2008, 01:06
reddogc22 reddogc22 is offline
New Member
 
Join Date: Oct 2008
Posts: 1
reddogc22 is on a distinguished road

Sort Long Integers Numerically then Lexically


I am having a hard time writing a program with C++ that will accept a series of long integers and print them out, first sorted numerically and then sorted lexically. A friend suggested I use strcmp(), but I am not sure how. I am pretty new to C++ so any help would be greatly appreciated.
  #2  
Old 02-Oct-2008, 03:04
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Sort Long Integers Numerically then Lexically


Quote:
Originally Posted by reddogc22
...writing a program with C++ that will accept a series of long integers and print them out, first sorted numerically and then sorted lexically. A friend suggested I use strcmp()...
The most elegant solution would be using qsort() which takes a function pointer to a user-defined function which handles comparisons, but newcomers may be confused by the syntax.

My suggestion would be:
  • Create a structure containing two fields: one a long int & the other a string. When the value is read, store it, & store its string equivalent via functions like sprintf().
  • Create an array of this structure & fill it with numeric values received as input along with its string equivalent.
  • Implement whatever sorting algorithm you choose, however, implement it twice: once for numeric long int values, & a second time for strings -- here, you would use a function like strcmp() to compare one string to another.
  #3  
Old 03-Oct-2008, 04:15
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Sort Long Integers Numerically then Lexically


Use std::sort().
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Median of Three Quick Sort Peter_APIIT C++ Forum 7 07-Oct-2008 03:39
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 21:33
Quick Sort Algorithm Peter_APIIT C++ Forum 3 03-Oct-2007 23:45
linked lists selection and insertion sort iceman2006 C++ Forum 1 27-Jun-2006 08:29
SORT / ORDER BY multi columns in MySQL misunderstood MySQL / PHP Forum 3 01-Oct-2003 10:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.