1

I am trying to sort a list of strings in an order that is pertinent to a sequence used in common revision history sequences. Some customers start with "-", or "New", or a few other specific codes, and I can use LINQ to define a custom sort order. However, after a drawing has been revised from Rev A all the way through Rev Z, it becomes Rev AA through Rev AZ, then Rev BA through BZ and so forth. How can I sort the following list using LINQ?

I have this as an example block of code:

List<string> revList = new List<string>  { "A", "NC", "New", "AB", "PD", "PD1", 
                                          "PD2", "B", "-", "*", "BB", "NA" };
revList = revList.OrderByDescending(i => i.ToLower() == "pd").
                  ThenByDescending(i => i.ToLower() == "nc").
                  ThenByDescending(i => i.ToLower() == "na").
                  ThenByDescending(i => i.ToLower() == "new").
                  ThenByDescending(i => i.ToLower() == "pd1").
                  ThenByDescending(i => i.ToLower() == "pd2").
                  ThenByDescending(i => i.ToLower() == "-").
                  ThenByDescending(i => i.ToLower() == "*").
                  ThenBy(i => i).ToList();

foreach (string rev in revList)
  Console.WriteLine(rev);

This produces output as:

PD
NC
NA
NEW
PD1
PD2
-
*
A
AB
B
B

I would like to produce:

PD
NC
NA
NEW
PD1
PD2
-
*
A
B
AB
BB
5
  • 2
    Sort by Length first, e.g. insert ThenBy(i => i.Length). before ThenBy(i => i) Commented Feb 22, 2017 at 21:39
  • 1
    Ivan, you win. I wish I could vote for your answer since it was first, but it was a comment. Thank You. Commented Feb 22, 2017 at 22:07
  • Why did PD come before NC is the desired results? Commented Feb 22, 2017 at 22:08
  • @JonathanWood Because of the first rule itself! Commented Feb 22, 2017 at 22:13
  • @JonathanWood Obscure details that are really only relevant to our specific numbering requirements. The pertinent part was that I could create a specific sequence of order using LINQ, but I couldn't figure out how to get the subsequent sequence to work in a non alphabetically. Commented Feb 22, 2017 at 22:14

2 Answers 2

4

This code worked for me.

 var revList = new List<string>  { "A", "NC", "New", "AB", "PD", "PD1",
                                      "PD2", "B", "-", "*", "BB", "NA" };

        revList = revList.OrderByDescending(i => i.ToLower() == "pd").
                          ThenByDescending(i => i.ToLower() == "nc").
                          ThenByDescending(i => i.ToLower() == "na").
                          ThenByDescending(i => i.ToLower() == "new").
                          ThenByDescending(i => i.ToLower() == "pd1").
                          ThenByDescending(i => i.ToLower() == "pd2").
                          ThenByDescending(i => i.ToLower() == "-").
                          ThenByDescending(i => i.ToLower() == "*").
                          ThenBy(i => i.Length).ToList();

        foreach (string rev in revList)
            Console.WriteLine(rev);
        Console.ReadLine();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Sameer! I cant believe I didn't think of that.
0

You could create a custom object like this:

class Revision
{
    public string Name { get; set; }
    public int Rank { get; set; }

    public Revision(string Name, int Rank)
    {
        this.Name = Name;
        this.Rank = Rank;
    }
}

Then you can create a list of these objects.

Something like:

List<Revision> revisions = new List<Revision>();
revision.Add(new Revision("PD",1));
revision.Add(new Revision("NC",2));

Now when you can sort by the rank using LINQ instead of trying to parse the names.

It may be a pain to set up the object initially since you may need to go out to ZZ, but this would give you complete control over the order.

p.s. - I didn't test this code.

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.