This will convert the string to a number when doing the comparison;
if (int.Parse(a.ElementAt(j)).CompareTo(int.Parse(a.ElementAt(j + 1))) > 0)
However it will error if you add somenone non-numeric to the list. For a number sort you're best using
LinkedList<int> x = new LinkedList<int>();
and changing the rest of the code as appropriate.
class Bubble
{
int i, j;
LinkedList<int> x = new LinkedList<int>();
public Bubble()
{
i = 0;
j = 0;
}
static void Main(string[] args)
{
Bubble ob = new Bubble();
int n = 10;
ob.x.AddLast(98);
ob.x.AddLast(34);
ob.x.AddLast(77);
ob.x.AddLast(24);
ob.x.AddLast(54);
ob.x.AddLast(50);
ob.x.AddLast(10);
ob.x.AddLast(6);
ob.x.AddLast(1);
ob.x.AddLast(83);
ob.bubblesort(ob.x, n);
Console.Read();
}
void bubblesort(LinkedList<int> a, int s)
{
for (i = 0; i < s; i++)
{
for (j = 0; j < s - 1; j++)
{
if (a.ElementAt(j).CompareTo(a.ElementAt(j + 1)) > 0)
{
LinkedListNode<int> current = a.Find(a.ElementAt(j));
var temp = current.Next.Value;
current.Next.Value = current.Value;
current.Value = temp;
}
}
}
Console.WriteLine("\n-----------------------");
Console.WriteLine("Sorted Linked List In Ascending Order Is=====>");
Console.WriteLine("-----------------------");
i = 1;
foreach (var item in a)
{
Console.Write("<" + (i++) + "> ");
Console.WriteLine(item);
}
}
}