Question

I've been searching the internet and books but have had no luck so was hoping someone could point me in the right direction.

I basically need to sort objects by their name statement alphabetically using an insertion sort rather than the built in methods. I have tried using an Array and List but can't seem to get it to work. How would you go about doing it?

I have a class of Player, latest attempt filled with list objects:

    public static List<Player> user = new List<Player>(); 
    private string name; //Read and Write
    private int score; //Read and Write
    private double health; //Read and Write
    private int level;  //Read and Write
    public string[] inventory = new string[30];

    public void setName(String newName)
    {
        name = newName;
    }
    public string getName()
    {
        return name;
    }
    public void setScore(int newScore)
    {
        score = newScore;
    }
    public int getScore()
    {
        return score;
    }
    public void setHealth(double newHealth)
    {
        health = newHealth;
    }
    public double getHealth()
    {
        return health;
    }
    public void setLevel(int newLevel)
    {
        level = newLevel;
    }
    public int getLevel()
    {
        return level;
    }

    public static void Saved_Player()
    {
        user.Add(new Player() { name = "Timid Bob", health = 63, level = 6, score = 2000, });
        user[0].inventory[0] = "Steel Sword";
        user[0].inventory[1] = "1mm MAW";
        user[0].inventory[2] = "Short Bow";
        user[0].inventory[0] = "Grenade";

        user.Add(new Player() {name = "Killer Bob", health = 82, level = 2, score = 1050000, });
        user[1].inventory[0] = "Glass Sword";
        user[1].inventory[1] = "250mm MAW";
        user[1].inventory[2] = "Elephant Bow";
        user[1].inventory[3] = "Rock";

etc... upto 6 user objects

To sort it I'm trying to use the following code in another class Form1:

//sudo code

          for(int i = 0; i < Player.user.Count; i++)
          {

            while (i index is higher than i+1 index)
            {
                swap i index with i+1 index
            }

          }  

Hope that is right :/

I think I understand how PublicJoe's has done it but how do you get and set the index of an object? Thanks for looking.

Was it helpful?

Solution

Arrays are bad for inserting into. if you think back to your classes, you might find a data structure that works better for inserting.

In an insertion sort, you take an item of your unsorted list, and then put it in the correct spot of the other list.

What you seem to be trying to do, appears to be some sort selection sort.

I think there's a problem with the 4 lines where you swap your values

                object temp;
                object = Player.user[Second];
                Player.user[first] = Player.user[Second];
                Player.user[(temp - 1)] = Player.user[Second];

I'd have a second look at that if i were you.

OTHER TIPS

If you're using a list, you can simply do this:

public void InsertionSort(Player newUser)
{
    var index = users.FindLastIndex(u => u.Name <= newUser.Name);
    users.Insert(index, newUser);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top