Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
First part: I wanted to set priority to my offers list. I need algorithm like this. If I want to place my 3rd offer to 1st place( 0 index ) then 3rd offer should be on 1st place and other offers move next we can see other offers get one index increment.
Second part: I want to place my 1st offer to 3rd place then existing 2nd and 3rd offer get 1 decrement and 1st placed on 3rd index.

What I have tried:

I am trying swapping algorithim in ASP.NET but failed
Posted
Updated 17-Nov-22 23:50pm
v2
Comments
PIEBALDconsult 17-Nov-22 8:11am    
In an array? A List? Some custom collection?
OriginalGriff 17-Nov-22 8:18am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Arsam Sameja 17-Nov-22 8:21am    
I am starting an array priority from 0,1,2,3,4,5,6,....n and now if i change 6 to 1 this should be swap and priority should be 0,6,2,3,4,5,1,....n
CHill60 17-Nov-22 8:32am    
Perhaps if you share the code you tried your question might become clearer. At the moment it is still very difficult to work out what you are trying to ask us for help with
Arsam Sameja 17-Nov-22 8:41am    
At this time I am trying this but this is not perfect for unlimited offers this work for 4 offers only


string priority = Request.Form["priority"].ToString();
string priorityindex = Request.Form["af"].ToString();
{
int[] prioritya;
prioritya = new int[4];
if (Convert.ToInt32(priority) == 4 && Convert.ToInt32(priorityindex) == 1)
{
prioritya[0] = 4;
prioritya[1] = 1;
prioritya[2] = 2;
prioritya[3] = 3;
}
if (Convert.ToInt32(priority) == 3 && Convert.ToInt32(priorityindex) == 1)
{
prioritya[0] = 3;
prioritya[1] = 1;
prioritya[2] = 2;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 2 && Convert.ToInt32(priorityindex) == 1)
{
prioritya[0] = 2;
prioritya[1] = 1;
prioritya[2] = 3;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 1 && Convert.ToInt32(priorityindex) == 1)
{
prioritya[0] = 1;
prioritya[1] = 2;
prioritya[2] = 3;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 4 && Convert.ToInt32(priorityindex) == 2)
{
prioritya[0] = 1;
prioritya[1] = 4;
prioritya[2] = 2;
prioritya[3] = 3;
}
if (Convert.ToInt32(priority) == 3 && Convert.ToInt32(priorityindex) == 2)
{
prioritya[0] = 1;
prioritya[1] = 3;
prioritya[2] = 2;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 2 && Convert.ToInt32(priorityindex) == 2)
{
prioritya[0] = 1;
prioritya[1] = 2;
prioritya[2] = 3;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 1 && Convert.ToInt32(priorityindex) == 2)
{
prioritya[0] = 2;
prioritya[1] = 1;
prioritya[2] = 3;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 4 && Convert.ToInt32(priorityindex) == 3)
{
prioritya[0] = 1;
prioritya[1] = 2;
prioritya[2] = 4;
prioritya[3] = 3;
}
if (Convert.ToInt32(priority) == 3 && Convert.ToInt32(priorityindex) == 3)
{
prioritya[0] = 1;
prioritya[1] = 2;
prioritya[2] = 3;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 2 && Convert.ToInt32(priorityindex) == 3)
{
prioritya[0] = 1;
prioritya[1] = 3;
prioritya[2] = 2;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 1 && Convert.ToInt32(priorityindex) == 3)
{
prioritya[0] = 2;
prioritya[1] = 3;
prioritya[2] = 1;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 4 && Convert.ToInt32(priorityindex) == 4)
{
prioritya[0] = 1;
prioritya[1] = 2;
prioritya[2] = 3;
prioritya[3] = 4;
}
if (Convert.ToInt32(priority) == 3 && Conver

Instead of using an array try using a List[^] instead. Then you can insert items at any position you like. You can also Remove[^] from any position.

If you absolutely want an array you can convert the list to an array with List<T>.ToArray Method (System.Collections.Generic) | Microsoft Learn[^] and an array to a list example is here Convert an Array to a List in C#[^]
 
Share this answer
 
Well here's a simple Move method which can do it, but there's gotta be a better way.
I don't recommend using something like this when there are a great many moves involved.

C#
public static bool
Move
(
  System.Collections.IList Array
,
  int                      From
,
  int                      To
)
{
  bool result = false ;

  switch ( From.CompareTo ( To ) )
  {
    case 1 : // Move to the Left
    {
      object val = Array [ From ] ;

      for ( int i = From ; i > To ; i-- )
      {
        Array [ i ] = Array [ i - 1 ] ;
      }

      Array [ To ] = val ;

      result = true ;

      break ;
    }

    case -1 : // Move to the right
    {
      object val = Array [ From ] ;

      for ( int i = From ; i < To ; i++ )
      {
        Array [ i ] = Array [ i + 1 ] ;
      }

      Array [ To ] = val ;

      result = true ;

      break ;
    }
  }

  return ( result ) ;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900