链表排序

来源:互联网 发布:动漫飞机杯推荐 知乎 编辑:程序博客网 时间:2024/05/24 15:36

/**
  * 将列表进行排序
  *
  * @param infoList
  */
 public void sortList(List<Info> infoList) {
  if (infoList != null && infoList.size() > 0) {
   for (int i = 1; i < infoList.size(); i++) {
    // 按面积比较
    if (infoList.get(i - 1).getArea() < infoList.get(i)
      .getArea()) {
     Info tempInfo = infoList.get(i);
     int j = 0;
     for (j = i - 1; j >= 0
       && infoList.get(j).getArea() < tempInfo
         .getArea(); j--) {
      infoList.set(j + 1, infoList.get(j));
     }
     infoList.set(j + 1, tempInfo);
    } else if (infoList.get(i - 1).getArea() == infoList.get(
      i).getArea()) {
     // 面积相等,按x比较
     Info tempInfo = infoList.get(i);
     int j = 0;
     for (j = i - 1; j >= 0; j--) {
      if (infoList.get(j).getX() > tempInfo.getX()) {
       // X升序
       infoList.set(j + 1, infoList.get(j));
      } else if (infoList.get(j).getX() == tempInfo.getX()) {
       // y升序
       for (j = i - 1; j >= 0
         && infoList.get(j).getY() > tempInfo
           .getY(); j--) {
        // y升序
        infoList.set(j + 1, infoList.get(j));
       }
      } else {
       break;
      }

     }
     infoList.set(j + 1, tempInfo);
    }
   }
  }

 }