2

I have a string that has a delimited format like this:

orgname: firstname lastname, firstname lastname;

(this can repeat with orgnames and variable number of names for each org)

Example:

**XXX University**: Martha Zander, Rick Anderson; **Albert School**: Nancy Vanderburg, Eric Towson, George Branson; **Hallowed Halls**: Jane Goodall, Ann Crabtree, Rick Grey, Tammy Hudson;

The resultant string needs to be grouped and sorted by orgname and then lastname like this:

**Albert School**: George Branson, Eric Towson, Nancy Vanderburg; 
**Hallowed Halls**: Ann Crabtree, Jane Goodall, Rick Grey, Tammy Hudson;
**XXX University**: Rick Anderson, Martha Zander;

I have gotten this far:

string[][] splitThis = staff.Split(';').Select(s => s.Split(':')).ToArray();

This gives me an array split up by orgname and then I am stuck after this.

I know I need a multidimensional array with orname, firstname, lastname but I don't know how to populate, group and sort it to get the proper result.

5
  • Why not use a sorted dictionary ? Commented Jun 15, 2015 at 21:06
  • Is there any chance that you will have a person that doesn't have exactly two parts to their name? Like Mary Jane Watson or Dale Earnhardt Jr.? Commented Jun 15, 2015 at 21:07
  • 1
    How do you define lastname. You have people with multiple first names, or with multiple last names. Like "William Of Baskeville", "Vlad the Impaler" and "Marie Rose Foo". Commented Jun 15, 2015 at 21:11
  • 3
    Why do you need a multidimensional array? This seems like a perfect time to define classes (like Person and Organization). Commented Jun 15, 2015 at 21:19
  • Unfortunately the data comes to us in this format by the client and was all hand-entered into one field of a database (sigh). We were told to sort it as best as possible for display. Therefore it would be possible to have multiple first or last names but I don't think there would be a way to determine and adjust for this. @ryanyuyu Commented Jun 15, 2015 at 21:48

2 Answers 2

2
public static void Main()
{
    var input =
    @"**Albert School**: George Branson, Eric Towson, Nancy Vanderburg; 
     **Hallowed Halls**: Ann Crabtree, Jane Goodall, Rick Grey, Tammy Hudson;
     **XXX University**: Rick Anderson, Martha Zander;";

    var universities = input
                      .Split(';')
                      .Select(ParseUniversity)
                      .ToArray();
}

public static University ParseUniversity(string line)
{
    var fields = line
                .Split(',',':')
                .Select(f=>f.Trim('*','\n','\r', '\t' ,' '))  // remove trailing crap
                .ToArray();

    var universityName = fields.First();

    var persons = fields
                 .Skip(1)  // skip university field
                 .Select(ParsePerson)
                 .ToArray();

    return new University {Name = universityName, Persons = persons};
}

public static Person ParsePerson(string field)
{
    var p = field.Split(' ');
    return new Person{FirstName = p.First(), LastName = p.Last()};
}

public class University
{
    public string   Name    {get;set;}
    public Person[] Persons {get;set;}
}

public class Person
{
    public string FirstName {get;set;}
    public string LastName  {get;set;}
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is just what I needed! The data is now organized perfectly. A million thanks for taking the time to help me.
1

I would use a regex here

string input = @"**XXX University**: Martha Zander, Rick Anderson; **Albert School**: Nancy Vanderburg, Eric Towson, George Branson; **Hallowed Halls**: Jane Goodall, Ann Crabtree, Rick Grey, Tammy Hudson;";
var dict = Regex.Matches(input, @"\*\*(.+?)\*\*: (.+?);")
           .Cast<Match>()
           .ToDictionary(x => x.Groups[1].Value, x=>x.Groups[2].Value.Split(','));

dict would be (in json):

{
  "XXX University": [
    "Martha Zander",
    " Rick Anderson"
  ],
  "Albert School": [
    "Nancy Vanderburg",
    " Eric Towson",
    " George Branson"
  ],
  "Hallowed Halls": [
    "Jane Goodall",
    " Ann Crabtree",
    " Rick Grey",
    " Tammy Hudson"
  ]
}

1 Comment

Thanks @EZI, That helps get the data into parsed into json format but I need to get it sorted and grouped by org and then sorted by last name. It's a good lesson for me how to use Dictionary and regex effectively. I appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.