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

を実装することをお勧めしComparatorます。Comparator オブジェクトに、昇順または降順のどちらで並べ替えるかを指定できるフィールドを設定してから、Collections.sort(maBibliotheque, new MyComparator(MyComparator.DESC))

デモ (必要に応じてジェネリックを調整します。この場合のように、特定のフィールドと比較していることがわかっている場合は、 を使用します。別の方法として、オブジェクトにo1.getField().compareTo(o2.getField())実装して単に を呼び出すこともできますが、それほど柔軟ではありません。 ComparableCollections.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 に答える
1

古いものから新しいものへと並べ替えてから、Collections.reverse(maBibliotheque);

于 2013-09-06T14:12:29.160 に答える