0

This seems to be simple enough, but I'm having a hard time getting free of the mental block on this one.

I've got a list of names and the items are ordered specifically based on some business rule. This is the order that is considered correct for some given situation.

So

string [] original = {"Bob","Jim","Kat","Nat","Kim","Ant"};

I've got a Person type that contains a field Name, which will have one of the above values.

So,

class Person {
    ...
    public  string Name; //Name will be one of the above values.
    ...
}

and I've got a bunch of these person objects, in a collection.

PersonCollection p = new PersonCollection 
                     { new Person{ .. Name = "Kat"..}, 
                       new Person {.. "Kim" ..} ...
                     } etc.;

I know that all of these objects will have names that only contain the values from the above list, there aren't even going to be duplicates. I need to arrange this collection of persons, by the order of the names supplied in the master list (i.e the "original" array above).

Straightforward so far, but here is the tricky bit.

By virtue of the way the Person Class is defined, and I cannot go in and change this, unfortunately, we only have the following available methods in the person object that

move the items.

MoveToFirst(PersonCollection collection) // Move to the first of the given collection
MoveToLast(PersonCollection collection) // Move to the last of the given collection
MoveAfter(PersonCollection collection, Person previousPerson) //places the object after another person item, which is present in the list.

Anyone? By the way, as you can probably tell, this is a "hypothetical" representaion of a problem, I cannot post or discuss the actual production code here, unfortunately. I hope someone else has encountered something similar before.

4
  • What you have problem with - sorting separate list of person OR re-ordering you collection by using just MoveAfter? Commented Jan 24, 2013 at 0:55
  • you have got to clean up this question .. I am not sure why you can't formulate a question that makes more sense without giving up company secrets if that's what you are afraid of.. I am getting dizzy looking at this mess Commented Jan 24, 2013 at 0:56
  • The issue is with re-organizing the collection using MoveAfter, based on the order of items supplied in the first array. Commented Jan 24, 2013 at 1:03
  • The key is to find the rank of a name, if it is a small collection of names use a Dictonary if it is large use database table with index. Commented Jan 24, 2013 at 1:06

1 Answer 1

1

So "Bob" < "Jim" < "Kat" etc.

Each person name has a rank. If the list of names is small, create a Dictonary Rank such that Rank["Bob"] < Rank["Jim"] etc.

Now create an IComparer that uses this data, the Map could actually be in the comparer itself. Use Array.Sort Method (Array, IComparer) to sort the persons using this comparer.

Sign up to request clarification or add additional context in comments.

Comments

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.