0

我想列出从最新日期到最早日期的日期。

我不想用Collections.sort(),所以我做了我自己的方法。

当我这样做时:

List<Livre> maBibliotheque = new ArrayList<Livre>();
boolean tri = false;
int born = maBibliotheque.size();            

             while (tri == false) 
             {
                 tri = true ;  
             for (int i=0; i<born-1;i++) 
             {
                 if ( maBibliotheque.get(i).getNewPeriode().compareTo(maBibliotheque.get(i+1).getNewPeriode())>0){
                     Livre livre = maBibliotheque.get(i);
                     maBibliotheque.set(i, maBibliotheque.get(i+1)); 
                     maBibliotheque.set(i+1,livre);  

                     tri = false ; }

             }
             born -= born;
             }

它完美地工作,但从最旧的日期到最新的日期,但我想要其他的。我将此行更改为

 if ( maBibliotheque.get(i).getNewPeriode().compareTo(maBibliotheque.get(i+1).getNewPeriode())<0){

但它什么也没做,在这种情况下没有排序日期。请帮忙

4

3 回答 3

2

要颠倒顺序,请替换> 0< 0

 born -= born;

做同样的事情

 born = 0;

我怀疑这不是必需的。


这个

public static void main(String[] args) throws IOException, IllegalAccessException, NoSuchFieldException {
    List<Livre> maBibliotheque = new ArrayList<Livre>();
    maBibliotheque.add(new Livre("aaa"));
    maBibliotheque.add(new Livre("abb"));
    maBibliotheque.add(new Livre("bbb"));
    maBibliotheque.add(new Livre("000"));
    boolean tri;
    int born = maBibliotheque.size();

    do {
        tri = true;
        for (int i = 0; i < born - 1; i++) {
            if (maBibliotheque.get(i).getNewPeriode().compareTo(maBibliotheque.get(i + 1).getNewPeriode()) > 0) {
                Livre livre = maBibliotheque.get(i);
                maBibliotheque.set(i, maBibliotheque.get(i + 1));
                maBibliotheque.set(i + 1, livre);
                tri = false;
            }

        }
    } while (!tri);

    System.out.println("increasing: " + maBibliotheque);

    do {
        tri = true;
        for (int i = 0; i < born - 1; i++) {
            if (maBibliotheque.get(i).getNewPeriode().compareTo(maBibliotheque.get(i + 1).getNewPeriode()) < 0) {
                Livre livre = maBibliotheque.get(i);
                maBibliotheque.set(i, maBibliotheque.get(i + 1));
                maBibliotheque.set(i + 1, livre);
                tri = false;
            }

        }
    } while (!tri);

    System.out.println("decreasing: " + maBibliotheque);
}

static class Livre {
    private final String newPeriode;

    Livre(String newPeriode) {
        this.newPeriode = newPeriode;
    }

    public String getNewPeriode() {
        return newPeriode;
    }

    @Override
    public String toString() {
        return "Livre{" +
                "newPeriode='" + newPeriode + '\'' +
                '}';
    }
}

印刷

increasing: [Livre{newPeriode='000'}, Livre{newPeriode='aaa'}, Livre{newPeriode='abb'}, Livre{newPeriode='bbb'}]
decreasing: [Livre{newPeriode='bbb'}, Livre{newPeriode='abb'}, Livre{newPeriode='aaa'}, Livre{newPeriode='000'}]
于 2013-09-06T14:13:57.483 回答
1

从最旧到最新排序,然后用Collections.reverse(maBibliotheque);

于 2013-09-06T14:12:29.160 回答
1

我建议实施一个Comparator. 您在 Comparator 对象中设置一个字段,可以告诉它是升序还是降序排序,然后调用Collections.sort(maBibliotheque, new MyComparator(MyComparator.DESC))

演示(根据需要调整泛型,如果在这种情况下,您知道您正在与特定字段进行比较 use o1.getField().compareTo(o2.getField())。或者,您可以Comparable在 Object 中实现并调用Collections.sort(List),但这并不灵活。

   public class MyComparator implements Comparator<String>
   {
      public static final int ASC = 0;
      public static final int DESC = 1; 

      private final int sortOrder;

      public MyComparator(int sortOrder)
      {
         this.sortOrder = sortOrder;
      }

      /* (non-Javadoc)
       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
       */
      @Override
      public int compare(String o1, String o2)
      {
         switch(this.sortOrder)
         {
            case ASC:
               return  o1.compareTo(o2);

            case DESC:
               return o2.compareTo(o1);
         }
         return 0;
      }
   }
于 2013-09-06T14:24:25.000 回答