Thursday, June 17, 2004

Array sorting example

private class Game
{
internal GameEnum GameEnum;
internal int Position;
internal Game(GameEnum gameEnum, int position)
{
GameEnum = gameEnum;
Position = position;
}
}

private class GameComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
// We are going to sort array of positions.
// We want to have the smallest position at the begining of sorted array
// We want "i1" value at the end of the array
Selector.Game gameX = (Game)x;
Selector.Game gameY = (Game)y;
int xPosition = gameX.Position;
int yPosition = gameY.Position;


if (xPosition == yPosition)
return 0;
if (xPosition == -1)
return 1; //Moves x to the end of the sorted array
if (yPosition == -1)
return -1; //Moves x toward beginning of the sorted array
if (xPosition < yPosition)
return -1; //Moves x toward beginning of the sorted array
else
return 1; //Moves x toward the end of the sorted array
}
}

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?