Sortowanie w sposob rosnacy i malejacy
Witam!!
Mam pewien problem,natomiast napisalem kod ktory ma za zadanie wyswietlic liste przebojow od 1 do 10 miejsca i to uzytkownik wybiera czy chce posortowac liste - malejaca czy rosnaco. Tu lezy problem bo za kazdym razem sortuje mi od 1 miejsca do ostatniego. Prosze o pomoc, z gory dziekuje.
KOD:
using System;
using System.Collections.Generic;
using System.Text;
class MainClass
{
public struct MyChart
{
public string RecordName;
public string ArtistsName;
public int PositionNow;
public int WeeksInCharts;
public int LastPosition;
public string Date;
}
public static void Main(string[] args)
{
Console.Write("How many position in the chart:");
int num = Int32.Parse(Console.ReadLine());
MyChart[] MyArrayOfStruct = new MyChart[10];
MyChart temp = new MyChart();
for (int i = 0; i < num; i++)
{
Console.Write("Enter Record Name:");
MyArrayOfStruct[i].RecordName = Console.ReadLine();
Console.Write("Enter Artist Name:");
MyArrayOfStruct[i].ArtistsName = Console.ReadLine();
Console.Write("Enter cuurent position:");
do
{
MyArrayOfStruct[i].PositionNow = Int32.Parse(Console.ReadLine());
if (MyArrayOfStruct[i].PositionNow < 1 || MyArrayOfStruct[i].PositionNow > 10)
Console.Write("Enter position again between 1 - 10 :\n");
} while (MyArrayOfStruct[i].PositionNow < 1 || MyArrayOfStruct[i].PositionNow > 10);
Console.Write("Enter how many weeks in Chart:");
MyArrayOfStruct[i].WeeksInCharts = Int32.Parse(Console.ReadLine());
Console.Write("Enter Last position on the chart:");
MyArrayOfStruct[i].LastPosition = Int32.Parse(Console.ReadLine());
Console.Write("Enter the date:");
MyArrayOfStruct[i].Date = Console.ReadLine();
}
Console.WriteLine("If you want to sort Chart in ascending order press 1");
Console.WriteLine("in descending order press 2");
Console.WriteLine("or if you want to quit press 3");
Console.Write("Enter your choice:");
int which = Int32.Parse(Console.ReadLine());
if (which == 1)
{
for (int a = 0; a < num; a++)
{
for (int i = 0; i < num - 1; i++)
if (MyArrayOfStruct[i].PositionNow > MyArrayOfStruct[i + 1].PositionNow)
{
temp = MyArrayOfStruct[i];
MyArrayOfStruct[i] = MyArrayOfStruct[i + 1];
MyArrayOfStruct[i] = temp;
return;
}
}
}
for (int i = 0; i < num; i++)
{
Console.WriteLine("The sorted values are\n " + MyArrayOfStruct[i].PositionNow + "\n" + MyArrayOfStruct[i].RecordName + "\n"
+ MyArrayOfStruct[i].ArtistsName + "\n" + MyArrayOfStruct[i].WeeksInCharts + "\n" + MyArrayOfStruct[i].LastPosition + "\n"
+ MyArrayOfStruct[i].Date);
}
if (which == 2)
{
for (int b = 0; b < num; b++)
{
for (int i = 0; i < num - 1; i++)
{
if (MyArrayOfStruct[i].PositionNow < MyArrayOfStruct[i + 1].PositionNow)
{
temp = MyArrayOfStruct[i];
MyArrayOfStruct[i] = MyArrayOfStruct[i + 1];
MyArrayOfStruct[i] = temp;
return;
}
}
}
for (int i = 0; i < num; i++)
{
Console.WriteLine("The sorted values are\n " + MyArrayOfStruct[i].ArtistsName + "\n" + MyArrayOfStruct[i].RecordName + "\n"
+ MyArrayOfStruct[i].PositionNow + "\n" + MyArrayOfStruct[i].WeeksInCharts + "\n" + MyArrayOfStruct[i].LastPosition + "\n"
+ MyArrayOfStruct[i].Date);
}
if (which == 3)
{
Console.WriteLine("End Chart");
Console.ReadLine();
}
}
}
}
Odpowiedzi: 1
Ale się omęczyłesz. dodajesz do kolekcjy List<>(albo tablicy) i używasz nazwaListu.Sort() bez parametrowy sortuje według alfabetu a z parametramy już poczytaj Sort(od , do , bool)
Strona 1 / 1