⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rowsortcomparator.java

📁 Java游戏高级编程!!很不错的!!!Java游戏高级编程!!很不错的
💻 JAVA
字号:
     package com.croftsoft.core.gui.table;

     import java.util.*;

     /*********************************************************************
     * Sorts rows based upon comparison of elements within a column.
     *
     * <p />
     *
     * @version
     *   2001-08-08
     * @since
     *   2001-08-07
     * @author
     *   <a href="http://croftsoft.com/">David Wallace Croft</a>
     *********************************************************************/

     public final class  RowSortComparator
       implements Comparator
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {

     private int      index;

     private boolean  reverse;

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * Main constructor.
     *********************************************************************/
     public  RowSortComparator (
       int      index,
       boolean  reverse )
     //////////////////////////////////////////////////////////////////////
     {
       this.index   = index;

       this.reverse = reverse;
     }

     /*********************************************************************
     * Convenience constructor.
     *
     * <pre>
     * this ( 0, false );
     * </pre>
     *********************************************************************/
     public  RowSortComparator ( )
     //////////////////////////////////////////////////////////////////////
     {
       this ( 0, false );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * The column index on which the rows are to be sorted.
     *********************************************************************/
     public void  setIndex ( int  index )
     //////////////////////////////////////////////////////////////////////
     {
       this.index = index;
     }

     /*********************************************************************
     * Reverses the sort order.
     *********************************************************************/
     public void  setReverse ( boolean  reverse )
     //////////////////////////////////////////////////////////////////////
     {
       this.reverse = reverse;
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * Compares two rows based upon column values.
     *
     * <p>
     * The row arguments should implement the java.util.List interface.
     * Row elements must implement the java.util.Comparable interface.
     * </p>
     *********************************************************************/
     public int  compare ( Object  o1, Object  o2 )
     //////////////////////////////////////////////////////////////////////
     {
       Comparable  e1 = ( Comparable ) ( ( List ) o1 ).get ( index );

       Comparable  e2 = ( Comparable ) ( ( List ) o2 ).get ( index );

       if ( ( e1 == null )
         && ( e2 == null ) )
       {
         return 0;
       }

       if ( e1 == null )
       {
         return reverse ? 1 : -1;
       }

       if ( e2 == null )
       {
         return reverse ? -1 : 1;
       }

       return e1.compareTo ( e2 ) * ( reverse ? -1 : 1 );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -