if i am given a name list ... how do i sort it alphabetically (from A to Z) ?? any idea ?
if i am given a name list ... how do i sort it alphabetically (from A to Z) ?? any idea ?
Search the board for 'sort'
It's a perenial favourite
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i browse through some threads .... i can do sorting if all the name is different from each other ...
the problem now is how to do sorting if some of the name share some part in common ... example
Amy Kong
Amy Lim
Joy
Joyce
any idea ?
i don't know anything about vector ... so please do not give explainations dealing with vectors ...
sort like you normally would, just walk through each string until you find which should go first...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
If you are using the STL string container, then '<' and '>' are defined and will take care of everything for you, just do a traditional sort (bubble, merge, STL sort function, whatever you choose). The only problem with this is if some words aren't capitalized since then "Bob" will come before "amy". Also, if you want to sort by last name then this won't work either. Both are easy to work around though with a little creativity
Last edited by PJYelton; 11-12-2003 at 10:00 AM.
If you don't want to use the sort function template, you can write your own sort function and use either the comparison operators for std::string, or strcmp from <cstring>.Code:#include <iostream> #include <algorithm> #include <string> int main() { std::string list[] = { "Joy", "Amy Lim", "Joyce", "Amy Kong" }; std::sort ( list, list + 4 ); for ( int i = 0; i < 4; i++ ) std::cout<< list[i] <<std::endl; }
My best code is written with the delete key.