I searched an answer for my question but I could not find anything. I am sorry if there is a similar theme. So I need to sort a list of objects by their property and I have to use bubble sort. How can I make it without using methods like "list.sort". Thank you in advance!
using System;
using System.Collections.Generic;
class Animals
{
public int id { get; set; }
public string name { get; set; }
public string color { get; set; }
public int age { get; set; }
}
class Program
{
static void Main()
{
Console.Write("How much animals you want to add?: ");
int count = int.Parse(Console.ReadLine());
var newAnimals = new List<Animals>(count);
Animals animal = new Animals();
newAnimals.Add(animal);
for (int i = 0; i < count; i++)
{
newAnimals[i].id = i;
Console.Write("Write name for animal: " + i + ": ");
newAnimals[i].name = Console.ReadLine();
Console.Write("Write age for animal: " + i + ": ");
newAnimals[i].age = int.Parse(Console.ReadLine());
Console.Write("Write color for animal: " + i + ": ");
newAnimals[i].color = Console.ReadLine();
newAnimals.Add(new Animals() { id = 1, name = "name" });
}
Console.WriteLine("Name \tAge \tColor");
for (int i = 0; i < count; i++)
{
Console.WriteLine(newAnimals[i].name + "\t" + newAnimals[i].age + "\t" + newAnimals[i].color);
}
Console.ReadLine();
}
}
Console.WriteLine("Name \tAge \tColor");
for (int i = 0; i < count; i++)
{
Console.WriteLine(newAnimals[i].name + "\t" + newAnimals[i].age + "\t" + newAnimals[i].color);
}
Console.ReadLine();
}
}