0

Say I have a list of object Person

{
  public string Name {get; set;}
  public string Gender {get; set;}
}

What I want is to create a List that I can order by using a property called OrderIndex. So I add this property to the Person object:

{
  public string Name {get; set;}
  public string Gender {get; set;}
  public int OrderIndex {get; set;}
}

The purpose of this OrderIndex property is to place the Person object in the list at a specific index for ranking the Person object higher.

But I am very concerned about the amount of logic that I have to implenent because of this. Because say I have 3 person objects

Name: Dave, Gender: Male, OrderIndex: 1 Name: Amanda, Gender: Female, OrderIndex: 2 Name: Trevor, Gender: Male, OrderIndex: 3

If I decrement the OrderIndex of Trevor, I will also have to make sure, that Amandas OrderIndex is incremented as to not have two person objects with the same OrderIndex.

I'm not sure if this question has been answered here before, but I haven't had any luck finding a specific thread to where this has been answered.

How should I approach this problem?

JoeCode
  • 15
  • 3
  • Do you really need the `OrderIndex` property? Can't you just have a `List`, and the index of a person would be the index of the instance in the array? – vyrp Jul 11 '19 at 10:46
  • Also, is `OrderIndex` going to be incremented and decremented only by 1? Or by any arbitrary value? If any value, I would recommend a `LinkedList` instead of `List`. – vyrp Jul 11 '19 at 10:47
  • The order has to be saved in a database, so that's why I need the OrderIndex property. – JoeCode Jul 11 '19 at 11:05

1 Answers1

0

If you change an OrderIndex you have to find the next integer which is free. So if you change Trevors OrderIndex to 2, you have to search the next free int for Amandas OrderIndex. So create a method where this is done.

Here's how you search next free int: get next available integer using LINQ

Presi
  • 806
  • 10
  • 26