Witam serdecznie. Staram się napisać program, który wczytuje z pliku ciąg liczb naturalnych a następnie je sumuje w odpowiedni sposób. Mam kawałek programu który wczytuje mi z pliku do tablicy elementy ale typu string. I sortowanie nie odbywa się tak jak powinno, bo jak np mamy liczby 1,2,6,11,7 to wynik sortowania wyglądać będzie następująco: 1,11,2,6,7. Zastanawiam się czy nie stworzyć na początku tablicy Int[] ale też do końca nie wiem jak :/ Proszę o ewentualne sugestie ;)
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
namespace System_array
{
public class SamplesArray
{
public static void Main()
{
String[] myArr = File.ReadAllLines("in.txt");
// Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:");
PrintIndexAndValues(myArr);
// Sorts the entire Array using the default comparer.
Array.Sort(myArr);
Console.WriteLine("After sorting the entire Array using the default comparer:");
PrintIndexAndValues(myArr);
}
public static void PrintIndexAndValues( String[] myArr )
{
for ( int i = 0; i < myArr.Length; i++ )
{
Console.WriteLine( " [{0}] : {1}", i, myArr[i] );
}
Console.WriteLine();
Console.ReadKey();
}
}
}
Fragment kodu jest zaczerpnięty ze stronki MSDN...