Skip to main content
GameDev.net gamedev.net
🔒 Locked

Alphabetic sorting

Started by lacsap Oct 12, 2001 at 12:36 PM 13 replies 800+ views
Original Post
lacsap
lacsap
Does anyone have a algorythm that allows you to alphabeticly sort 10-20 names that are entered by a person ? This MUST be in C++ ! thanks in advance
Nytegard
Nytegard
Heap Sort
Quick Sort
Bucket Sort
Radix Sort

There are a lot more. All of these can be done in C++ easily.
noparity
noparity
Do a search for bubble sort. It''s not the most efficient algorithm, but for 10-20 items it should be fine.
lacsap
lacsap
Thanks for your help, I''ll go for the bubble sort algorythm

pascal
Stoffel
Stoffel
std::lexicographical_compare & your favorite container''s version of sort.
BeerNutts
BeerNutts
I wrote a program for someone and it does this exact thing. SortEmployeeData sorts a structure by age, then by last name. IsLessWord tells if word 1 is less alphabetically than word2

  
/********************************************************
*
* SortEmployeeData - Sorts the employee data by age
*
*********************************************************/
void SortEmployeeData(tEmployee *Employees, int NumEmployees)
{
int i, j;
tEmployee TempEmployee;

// Perform Bubble sort out the array
for( i = 0 ; i < NumEmployees ; i++ )
{
for( j = i+1; j < NumEmployees ; j++ )
{
if ( Employees[i].Age >= Employees[j].Age )
{
// if equal, then compare last name
if ( Employees[i].Age == Employees[j].Age )
{
// If the name is 1st alpha-wise, then don''t swap
if ( IsLessWord(Employees[i].cLastName, Employees[j].cLastName ))
{
continue;

} /* endIF */

} /* endIF */
// Swap these two places
TempEmployee = Employees[i];
Employees[i] = Employees[j];
Employees[j] = TempEmployee;

} /* endIF */

} /* endFor */

} /* endFor */


} // endof SortEmployeeData

/********************************************************
*
* IsLessWord - Is the 1st word less alpha-wise than second
*
*********************************************************/
int IsLessWord(char *cName1, char *cName2)
{
int Count = 0;
int IsLess = 0;

// loop through comparing letters
while((cName1[Count] != 0) && (cName2[Count] != 0))
{
if ( tolower(cName1[Count]) < tolower(cName2[Count]) )
{
// yes!
IsLess = 1;
break;

} /* endIF */
else if ( tolower(cName1[Count]) > tolower(cName2[Count]) )
{
// Nope
break;

}

// otherwise, continue to next letter
Count++;
}

// if we reached end of name1, then it must be lower
if ( cName1[Count] == 0 )
{
IsLess = 1;

} /* endIF */


return (IsLess);

} // endof IsLessWord

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Gladiator
Gladiator
well since beernuts already revealed the solution why i dont make it simple so all of us learn something here..

instead of writing your own comparision function there''s already strcmp() which can give you that information... take a look at strcmp() reference.. it returns 0 if the strings are the same but it can give you >0 or <0 depending on the strings'' alphabetical order... once agian it uses bubble sort..the easiest to code (good for a small number of items)

  
void sortNames( char names[][MAX_STRING_LENGTH], const int size )
{
int done;
do
{
done = 1;
for( int i = 0; i < size-1; i++ )
{
if( strcmp( names[i], names[i+1] ) > 0 )
{
swapNames( names[i], names[i+1] );
done = 0;
}
}
} while( !done );
}


hope this helps

-------------------------------
Did someone say "banana"?

..-=gLaDiAtOr=-..
BeerNutts
BeerNutts
The only problem with Gladiator''s solution is strcmp doesn''t account for capital letters.

The string "Zipper" would be considered 1st alphabetically over the string "apple" (since ''Z'' = 90 and ''a'' = 97).

Nutts
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
S1CA
S1CA
strcmp() also doesn''t take locales into account! It operates purely on an ASCII basis. In ASCII non-English/Roman characters come after all the English/Roman ones so sorting on many European names would be incorrect!.

1) For comparisons which take the locale into account use the strcoll() function.

2) To also do the comparison in a case-insensitive way, use _stricoll().


If you need to do comparisons on Japanese, Chinese, Korean or Arabic character sets use the wide/multi character versions (_wcsicoll or _mbsicoll)

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
lacsap
lacsap
Can''t I just convert the whole string to lowercase ?
lacsap
lacsap
I finally got the program to work !!!
I now have written my own function that checks if to strings are equal or not, a function that converts a string to lowercase characters (to avoid the ASCII trouble), and a swap function.

thanks for your help, everyone who helped me !

o0*pascal-lacsap*0o
djsteffey
djsteffey
personally i would use the built in quicksort in c/c++.
just look up qsort(........);


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.