C
C#•9mo ago
Dyad

Better approach to grouping and ordering items?

Should containers be in charge of ordering, or the items themselves?

Approach 1:

class Item
{
string GroupId;
int GroupOrder;
}

var items = [...];
var groups = items.orderBy(x => x.GroupOrder).groupBy(x => x.GroupId);

This approach seems tedious if I want to reorder items, since I might have to update many items GroupOrder when moving one.

Approach 2:

class Group
{
string Id;
List<Item> Items;
}

var groups = new List<Groups>
{
new Group { Id = "group.one", Items = ... },
new Group { Id = "group.two", Items = ... },
...
}

This approach seems easier to move items around, but involves manual entry into the Lists themselves
Should containers be in charge of ordering, or the items themselves?

Approach 1:

class Item
{
string GroupId;
int GroupOrder;
}

var items = [...];
var groups = items.orderBy(x => x.GroupOrder).groupBy(x => x.GroupId);

This approach seems tedious if I want to reorder items, since I might have to update many items GroupOrder when moving one.

Approach 2:

class Group
{
string Id;
List<Item> Items;
}

var groups = new List<Groups>
{
new Group { Id = "group.one", Items = ... },
new Group { Id = "group.two", Items = ... },
...
}

This approach seems easier to move items around, but involves manual entry into the Lists themselves
12 Replies
Angius
Angius•9mo ago
Approach 3: use approach 2 but load the data from a file
Dyad
DyadOP•9mo ago
What does that mean? The things I'm trying to order are defined in code, and are not user objects / rows
Angius
Angius•9mo ago
Do they have to be defined in code? Or are they just data
Dyad
DyadOP•9mo ago
I guess you could define them in a csv file or something but that still begs the question would the item rows have an order column or would the group table have a column for List of item ids which resource should control the order
Angius
Angius•9mo ago
I'd say it depends: are the items the focus, or are the groups? If the items are the focus, I'd say give them the group ID, order, and so on Akin to, say, a blogpost having tags and a category The second approach would be useful if an item can belong to multiple groups Since you can have the same reference in multiple of them
Dyad
DyadOP•9mo ago
I see, how about the problem of re-ordering in approach one?
Angius
Angius•9mo ago
You would have to edit something anyway In the first example, you change a 5 to a 6
Dyad
DyadOP•9mo ago
If I wanted to move the Last Item to the front, I would have to increment all items order?
Angius
Angius•9mo ago
In the second, you would have to cut-and-paste blocks of code between groups
Dyad
DyadOP•9mo ago
Would you ever use the second approach in a one to many relationship?
Angius
Angius•9mo ago
Hard to say, tbh. I'd just store those items in a proper database and load them into either structure depending on my needs
Dyad
DyadOP•9mo ago
Gotcha, thanks! 🙂
Want results from more Discord servers?
Add your server