Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to numerically order array of delimited strings in C#

Tags:

arrays

c#

I'm in a little bit of a bind. I'm working with a legacy system that contains a bunch of delimited strings which I need to parse. Unfortunately, the strings need to be ordered based on the first part of the string. The array looks something like

array[0] = "10|JohnSmith|82";
array[1] = "1|MaryJane|62";
array[2] = "3|TomJones|77";

So I'd like the array to order to look like

array[0] = "1|MaryJane|62";
array[1] = "3|TomJones|77";
array[2] = "10|JohnSmith|82";

I thought about doing a 2 dimensional array to grab the first part and leave the string in the second part, but can I mix types in a two dimensional array like that?

I'm not sure how to handle this situation, can anyone help? Thanks!

like image 569
Wil Avatar asked Jan 21 '10 20:01

Wil


4 Answers

Call Array.Sort, but passing in a custom implementation of IComparer<string>:

// Give it a proper name really :)
public class IndexComparer : IComparer<string>
{
    public int Compare(string first, string second)
    {
        // I'll leave you to decide what to do if the format is wrong
        int firstIndex = GetIndex(first);
        int secondIndex = GetIndex(second);
        return firstIndex.CompareTo(secondIndex);
    }

    private static int GetIndex(string text)
    {
        int pipeIndex = text.IndexOf('|');
        return int.Parse(text.Substring(0, pipeIndex));
    }
}

Alternatively, convert from a string array into an array of custom types by splitting the string up appropriately. This will make life easier if you're going to do further work on the array, but if you only need to sort the values, then you might as well use the code above.

You did say that you need to parse the strings - so is there any particular reason why you'd want to parse them before sorting them?

like image 56
Jon Skeet Avatar answered Oct 28 '22 09:10

Jon Skeet


        new[] {
            "10|JohnSmith|82",
            "1|MaryJane|62",
            "3|TomJones|77",
        }.OrderBy(x => int.Parse(x.Split('|')[0]));
like image 45
Jay Bazuzi Avatar answered Oct 28 '22 08:10

Jay Bazuzi


Use an ArrayList (http://msdn.microsoft.com/en-us/library/system.collections.arraylist_methods(VS.80).aspx) so you can sort it.

like image 1
Ryan Miller Avatar answered Oct 28 '22 08:10

Ryan Miller


If the array is large, you will want to extract the initial integers all in one pass, so you are not parsing strings at every comparison. IMO, you really want to encapsulate the information encoded in the strings into a class first. Then sort the array of those objects.

Something like:

class Person {
  int Index { get; }
  string Name { get; }
  int Age { get; }  // just guessing the semantic meaning
}

So then:

  1. Map your encoded string into an ArrayList of Person objects.
  2. Then use ArrayList.Sort(IComparer) where your comparer only looks at the Index.

This will likely perform better than using parse in every comparison.

like image 1
Dagititis Avatar answered Oct 28 '22 08:10

Dagititis