Skip to main content
deleted 16 characters in body
Source Link
CodesInChaos
  • 5.9k
  • 4
  • 24
  • 28

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[]List<int> parts1 = s1.Split('.').Select(int.Parse).ToArrayToList();
    int[]List<int> parts2 = s2.Split('.').Select(int.Parse).ToArrayToList(); 

    ifwhile(parts1.LengthCount !=< parts2.LengthCount)
        throwparts1.Add(0);
 new FormatException  while(parts2.Count < parts1.Count)
        parts2.Add(0); 

    for(int i = 0; i < parts1.Length;Count; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[] parts1 = s1.Split('.').Select(int.Parse).ToArray();
    int[] parts2 = s2.Split('.').Select(int.Parse).ToArray();
    if(parts1.Length != parts2.Length)
        throw new FormatException();
    for(int i = 0; i < parts1.Length; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    List<int> parts1 = s1.Split('.').Select(int.Parse).ToList();
    List<int> parts2 = s2.Split('.').Select(int.Parse).ToList(); 

    while(parts1.Count < parts2.Count)
        parts1.Add(0);
    while(parts2.Count < parts1.Count)
        parts2.Add(0); 

    for(int i = 0; i < parts1.Count; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}
added 34 characters in body
Source Link
CodesInChaos
  • 5.9k
  • 4
  • 24
  • 28

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[] parts1 = s1.Split('.').Select(int.Parse).ToArray();
    int[] parts2 = s2.Split('.').Select(int.Parse).ToArray();
    if(parts1.Length != parts2.Length)
        throw new FormatException();
    for(int i = 0; i < parts1.Length; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}
void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[] parts1 = s1.Split('.').Select(int.Parse).ToArray();
    int[] parts2 = s2.Split('.').Select(int.Parse).ToArray();
    if(parts1.Length != parts2.Length)
        throw new FormatException();
    for(int i = 0; i < parts1.Length; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[] parts1 = s1.Split('.').Select(int.Parse).ToArray();
    int[] parts2 = s2.Split('.').Select(int.Parse).ToArray();
    if(parts1.Length != parts2.Length)
        throw new FormatException();
    for(int i = 0; i < parts1.Length; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[] parts1 = s1.Split('.').Select(int.Parse).ToArray();
    int[] parts2 = s2.Split('.').Select(int.Parse).ToArray();
    if(parts1.Length != parts2.Length)
        throw new FormatException();
    for(int i = 0; i < parts1.Length; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}
Source Link
CodesInChaos
  • 5.9k
  • 4
  • 24
  • 28

Your problem is that your "numbers" don't have decimal places. You have a string which consists of two integers which are separated by .. Parse them as two integers and write a custom comparer. The sorting algorithm can remain the same, you just need to pass in the comparer.

In C# such a comparer could look similar to this:

void Test()
{
    var data=new string[]{ "1.34", "1.15", "1.1", "1.2", "1.26", "1.10", "1.20", "1.17" };
    Array.Sort(data, VersionComparer);
    data.Dump();
}

static int VersionComparer(string s1, string s2)
{
    int[] parts1 = s1.Split('.').Select(int.Parse).ToArray();
    int[] parts2 = s2.Split('.').Select(int.Parse).ToArray();
    if(parts1.Length != parts2.Length)
        throw new FormatException();
    for(int i = 0; i < parts1.Length; i++)
    {
         if(parts1[i] < parts2[i])
             return -1;
         if(parts1[i] > parts2[i])
             return +1;
    }
    return 0;
}