Reorder numerical sequence when changing array position

Asked

Viewed 47 times

-1

I have a list of financial categories

My model

public class Categoria 
{
   public Guid Id {get;set;}
   public string Nome {get;set;}
   public int Sequencia {get;set;}
}

And as you move them up and down you need to update the Sequence property

var oldIndex = categoria.Sequencia;
var newIndex = categoria.Sequencia + 1 //Para cima
var newIndex = categoria.Sequencia - 1 //Para baixo

But I have no idea how to reorder the Sequencia field by updating its codes

Example, initial list:

1 - ÁGUA
2 - LUZ,
3 - Internet

You give a Up of the Internet, then it should update and stay in the following sequence

1 - ÁGUA
2 - Internet
3 - Luz

What I’ve tried so far:

    var categoria = await db.categorias.FirstOrDefaultAsync(x => x.Id == id);

    var categorias = await db.categorias.ToListAsync();

    var oldIndex = categoria.Sequencia;
    var newIndex = direcao == "UP" ? categoria.Sequencia + 1 : categoria.Sequencia - 1;

    foreach (var cat in categorias.Where(x => x.Sequencia > newIndex && x.Sequencia <= oldIndex && x.Id != categoria.Id))
    {
        cat.Sequencia = cat.Sequencia --;
    }
    category.Sequencia = newIndex;
  • I’m sorry, I couldn’t understand your doubt

  • @Leandroangelo would be to reorder the Dice when it is passed Up or Down

  • But if the user clicks 10 times in a row on the up or down, will you do this process of going into the bank 20 times (since you go twice in the bank in this current code, 2 times * 10 clicks = 20) to update the sort? Perhaps you could change the flow to order everything by clicking up or down (but not yet apply the update in the database) and when the user clicks a button to apply the ordering, only in this case you perform the modifications in the database. What do you think?

  • @Pedropaulo the list usually does not exceed 30 items, even searching every hour is with a UX/UI more accessible for the user to understand

1 answer

1

As I mentioned in the comments, I would say to evaluate a better way to do this, because if the user clicks several times on the button, it is several calls to the database. I can’t suggest anything better here by message and without seeing your UI scenario, but surely there is a more efficient way to do this.

But I will try to help you in your current scenario.

var categoriaModificada1 = await db.categorias.FirstOrDefaultAsync(x => x.Id == id);

if (categoriaModificada1 != null)
{
    var novaSequencia = direcao == "UP" ? categoriaModificada1.Index - 1 : categoriaModificada1.Index + 1;
    var categoriaModificada2 = await db.categorias.FirstOrDefaultAsync(x => x.Index == novaSequencia);

    if (categoriaModificada2 != null)
    {
        categoriaModificada2.Index = categoriaModificada1.Index;
        categoriaModificada1.Index = novaSequencia;
        db.SaveChanges();            
    }
}

I made this code snippet based on your current scenario and your example above:

1 - ÁGUA
2 - LUZ,
3 - Internet

You give an Up from the Internet, then you should update and stay on the following sequence

1 - ÁGUA
2 - Internet
3 - Luz
  • Thank you Pedro, but what if it’s recursive?

  • I don’t understand. What would be recursive? This form that is done, is for your scenario, when the user clicks up or down, will change the ordering of the 2 items. This code works for every click.

  • Pedro, your solution is only to organize 2 of the items, and these items are dynamic, can have 2 or 50 for example rsrs

  • Yes, because from what I understand, you call the backend every time the user clicks on the UI, so you will always reorder 2 items. When the user clicks up or down, only 2 items are being changed. If it clicks 2 times, you will call this above method 2 times, so it will adjust correctly. That’s what I suggested earlier tidying up... Find a solution to do only once, but this has to analyze the project and I can’t do here with just one example, I need to understand the screen where this applies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.