# Sorting string array using selection sort algorithm

Page 1 of 1

## 1 Replies - 9594 Views - Last Post: 28 October 2009 - 05:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=134849&amp;s=59fb5687edfdda76a244b89d130d4d52&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jnCPP

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 27-October 09

# Sorting string array using selection sort algorithm

Posted 27 October 2009 - 03:10 PM

Hi, I'm new here, so forgive me if I blatant violate any rules..
My question, I believe, is rather simple.
From the C++ code below (Simple selection sort of 30 elements to put them in ascending order)
I have to implement an alphabetical version of this for the player names. (30 elements, blackhawk players roster)
What I need help with is how to compare the strings, do I approach it the same as I would an array of int?
I know there are builtin functions to help with this, but I was wondering how to get the first char of each string array element and compare it. I also have to write 9 more lines of swap logic for all the goals/shots/assists, but I can manage that.
```int top = 0,
last = numPlayers,
ssf,
ptr,
temp;
string swap;

do
{
ptr = top;
ssf = top;
do
{
if (goals[ptr] < goals[ssf])
{
ssf = ptr;
}
ptr = ptr + 1;
}
while (ptr < last);
temp = goals[top];
goals[top] = goals[ssf];
goals[ssf] = temp;
```

Is This A Good Question/Topic? 0

## Replies To: Sorting string array using selection sort algorithm

### #2 brds

• D.I.C Addict

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: Sorting string array using selection sort algorithm

Posted 28 October 2009 - 05:48 AM

You can access the individual characters as if it was an array.
```string temp = "Apple";
cout << temp[0] << endl; /* Print first character */

```

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }