Sorting string array using selection sort algorithm

I don't know how to compare the first char in each element

Page 1 of 1

1 Replies - 9182 Views - Last Post: 28 October 2009 - 05:48 AM Rate Topic: -----

#1 jnCPP  Icon User is offline

  • New D.I.C Head

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

Sorting string array using selection sort algorithm

Post icon  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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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