-2

I am trying to create a sort function that intelligently sorts our embroidery design schedule. Orders have Id's and might have 2 or 3 designs per order. Each list entry corresponds to one design. Designs with the same name may also appear across multiple orders, so design 'Manchester Utd Crest' may appear on order 123456 and order 55555

I want to sort by the design names first (string) and then by OrderId's (int). So I want all my design names (e.g. Manchester Utd Crest) to stay together on our schedule. However, if there is another design on that same order, I want the second design in the order to appear below the first design, even though it has a different name.

So I would end up with something like this:

Table Of Sorted Schedule Items

Here is my code so far: it only sorts by OrderId, then by Design Name. I need it to sort by Design Names, but also make sure OrderIds that are the same always stay together in the list as well, which it doesn't do right now:

embroideryJobs.Sort((EmbroideryJob a, EmbroideryJob b) =>
{
    if (a.OrderId != b.OrderId)
        return a.OrderId.CompareTo(b.OrderId);

    if (a.DesignProcessData[0].Name != b.DesignProcessData[0].Name)
        return a.DesignProcessData[0].Name.CompareTo(b.DesignProcessData[0].Name);

    return 0;
});
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    You should provide what you've tried already. – MakePeaceGreatAgain Oct 29 '21 at 11:19
  • Take a look at ``OrderBy` – Neil Oct 29 '21 at 11:57
  • 1
    I think that [LINQ OrderBy versus ThenBy](https://stackoverflow.com/questions/3760001/linq-orderby-versus-thenby) might be what you're looking for. – Andrew Morton Oct 29 '21 at 12:21
  • How do you select which design (of the same order ID) is the one used for the name-based sort? E.g. should order 123456 be ordered on M or on N? This is unclear. Without picking a specific design name to order by, you're really just ordering by order ID. – Flater Nov 02 '21 at 18:02
  • _I want to sort by the design names first (string) and then by OrderId's (int)_. This contradicts your explanation. If that were the case, elements of the same name would stick together, not elements with the same order ID. – Flater Nov 02 '21 at 18:07
  • Its really hard to explain what I want, but note the order of the entries in the sample list I provided. Entries with the same order id appear after each other, but if they don't have the same order id, then it looks for the same design name to place after it. If there isn't an entry with the same design name, then it looks at sorting by the next order id in the list. Maybe that's a clearer explanation of what I am trying to achieve? – Rob_Da_Joyce Nov 02 '21 at 21:22

1 Answers1

0

It sounds like you're wanting to sort by more than one criteria. You can do that with the LINQ functions OrderBy and ThenBy. So, you would want to sort like this:

var sortedJobs = embroideryJobs.OrderBy(x => x.OrderId).ThenBy(x => x.DesignName);

If embroideryJobs contained this:

OrderId DesignName
123456  Manchester Utd Crest
987654  Manchester Utd Crest
123456  Name & Number
5555555 Name & Number
5555555 Oldham Athletic Crest

sortedJobs would contain this:

OrderId DesignName
123456  Manchester Utd Crest
123456  Name & Number
987654  Manchester Utd Crest
5555555 Name & Number
5555555 Oldham Athletic Crest

Also, be sure you having a using statement that pulls in the System.Linq namespace:

using System.Linq;

at the top of the file.

Jason Williams
  • 1,283
  • 2
  • 11
  • 31
  • I'm pretty sure that would sort all the orderids in order like 123456, 123456, 555555, 555555, 987654. what I want it to do is prioritise Design Names, but always keep orders together that have more than one design, and then make it so that if there is another order with the same design name in the schedule, the next order in the list would be that order. Keeping all my orders together, but also my design names. – Rob_Da_Joyce Nov 02 '21 at 21:17
  • @Rob_Da_Joyce I'm not sure I'm understanding the problem. Can you provide a larger data set that demonstrates the problem with this solution? – Jason Williams Nov 03 '21 at 00:12