Sorting string

This is a discussion on Sorting string within the C++ Programming forums, part of the General Programming Boards category; if i am given a name list ... how do i sort it alphabetically (from A to Z) ?? any ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    Sorting string

    if i am given a name list ... how do i sort it alphabetically (from A to Z) ?? any idea ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    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 ...

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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;
    }
    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>.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting 2D string array
    By sureshhewa in forum C Programming
    Replies: 14
    Last Post: 07-27-2008, 02:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21