排序排序

时间:2019-02-09 21:11:32

标签: java sorting

我有六个项目的数组(代表事物的结构):

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

我还有另一个用于开关的变量。这告诉我哪个“事物”应该在第一位置。

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

当用户选择其他位置(例如3)时,“内容”应按以下顺序排序:

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

我已经弄清楚了。问题在于每个“事物”还具有status的属性。如果statusbroken,那么我需要将其推到底并进行调整。

示例thing4broken,而thing1first

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

这有意义吗?

我的第一次订购是这样的:

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;
    }
}

第二部分如下:

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

因此,似乎我可以轻松检查数组的两端。我基本上从“主要”选择器的索引开始。一旦得到这个,我就抵消了idxBack。然后,我循环并减少了后向索引,并增加了前向索引(idxBack)。检查条件后,可以轻松进行调整。这使我发疯了很长时间。

 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;
  }