newGuy
newGuy

Reputation: 354

Sorting with a twist

I have an array of six items (structs that represent a thing):

thing[] = new thing{thing1,thing2,thing3,thing4,thing5,thing6};

I have another variable that I use for a switch. This tells me which "thing" should be in the first position.

ENUM positionDesignation {first=0,second=1 ...sixth=5} 
//declared a little different.

When a user selects a different position, say 3, then the "things" should be sorted by:

  1. thing3
  2. thing4
  3. thing5
  4. thing1
  5. thing2

I have this figured out. The problem is that each "thing" also has a property of status. If status is broken, then I need to push it to the end and adjust.

Example: thing4 is broken and thing1 is first

thing = {
  thing1.first,
  thing2.second,
  thing3.third,
  thing4.sixth,
  thing5.fourth,
  thing6.fifth
};

Does this make sense?

My first ordering looks like this:

public void setRotationOrder() {
    Thing[] things = {
        getThing1(),
        getThing2(),
        getThing3(),
        getThing4(),
        getThing5(),
        getThing6()
    };

    int[] wheel = new int[6];
    int idx = getPrimary().getOrdinal();

    idx = (idx == 0) ? 5 : idx - 1;

    for (int i = 5; i >= 0; i--) {
        wheel[idx] = i;
        Thing[i].setState(Rotation6Designation.make(idx));
        idx = (idx == 0) ? 5 : idx - 1;
    }
}

The second part looks like this:

int lastIndex = 5;
int cnt = 0;
int thingPos = 0;

for (int i=5;i>=0;i--) {
    // Check thing at idx. If out of service, put it at the end.
    thingPos = wheel[i];

    if (things[thingPos].getStatus().equals(AlarmShutdown) ||
            things[thingPos].getStatus().equals(MaintenanceShutdown)) {
        if (i == lastIndex) {
            things[thingPos].setState(Rotation6Designation.make(lastIndex));
            lastIndex--;
            continue;
        }

        int tmpThingPos = thingPos;
        thingPos++;

        while (true) {
            things[thingPos].setState(things[thingPos + 1].getState());
            thingPos++;

            if (thingPos == lastIndex) {
                things[thingPos].setState(Rotation6Designation.make(tmpThingPos));
            }

            lastIndex--;
            break;
        }
    }
}

Upvotes: 0

Views: 62

Answers (1)

newGuy
newGuy

Reputation: 354

So, it seems as though I could easily check both ends of the array. I basically started at the index of the "primary" selector. Once I got this, I offset the idxBack. I then looped and reduced the back index and increased the front index (idxBack). After checking conditions, it was easily adjusted. This drove me nuts for a long time.

 public void setRotationOrder(){
  Thing[] things = {getThing1(),getThing2(),getThing3(),getThing4(),getThing5(),getThing6()};
  int idxFront = getPrimary().getOrdinal(); //3
  int idxBack =(idxFront==0)?5:idxFront-1; //2

  int lastIndex =5;
  int firstIndex=0;
  for (int i =5 ;i>=0 ;i--){
    System.out.println("Last index: "+lastIndex+", First index: "+firstIndex);
    System.out.println("idxBack: "+idxBack+", idxFront: "+idxFront);
    //*********************NEW*******************************/

    if (things[idxBack].getStatus().equals(Mode.AlarmShutdown) ||
            things[idxBack].getStatus().equals(Mode.MaintenanceShutdown)) {
      things[idxBack].setState(RotationDesignation.make(lastIndex));
      System.out.println( "Broken: things["+idxBack+"] "+things[idxBack].getState());
      System.out.println( "things["+idxBack+"] "+things[idxBack].getStatus());
      lastIndex--;
    }
    if (!things[idxFront].getStatus().equals(Mode.AlarmShutdown) &&
            !things[idxFront].getStatus().equals(Mode.MaintenanceShutdown)) {
      things[idxFront].setState(RotationDesignation.make(firstIndex));
      System.out.println( "Not Broken: things["+idxFront+"] "+things[idxFront].getState());
      firstIndex++;

    }
    idxFront=(idxFront==5)?0:idxFront+1;
    idxBack=(idxBack==0)?5:idxBack-1;
  }

Upvotes: 1

Related Questions