排序是編程中經常要碰到的問題
首先讓我們來看一下用戶需求是怎麼樣(為了更容易理解我將用戶需求簡化了一下
對於這個需求我們第一個想到的應該有一個員工信息對象(Employee)
下面我們來看一下具體代碼:
Employee對象
/** */ /**
*
*/
package com
import java
/** */ /**
* @author Administrator
*
*/
public class Employee
private Long employeeId;
private String firstName;
private String lastName;
private Date birthday;
private BigDecimal payment;
public Date getBirthday()
return birthday;
}
public void setBirthday(Date birthday)
}
public Long getEmployeeId()
return employeeId;
}
public void setEmployeeId(Long employeeId)
}
public String getFirstName()
return firstName;
}
public void setFirstName(String firstName)
}
public String getLastName()
return lastName;
}
public void setLastName(String lastName)
}
public BigDecimal getPayment()
return payment;
}
public void setPayment(BigDecimal payment)
}
@Override
public int hashCode()
// TODO Auto
return super
}
@Override
public String toString()
StringBuffer buf = new StringBuffer();
buf
}
}
Employee的complarator對象
/** */ /**
*
*/
package com
import java
/** */ /**
* @author Administrator
*
*/
public class EmployeeComparator implements Comparator
ISortOrder sortOrder;
public EmployeeComparator(ISortOrder sortOrder)
}
/**/ /* (non
* @see java
*/
public int compare(Object arg
}
}
/** */ /**
*
*/
package com
/** */ /**
* @author Administrator
*
*/
public interface ISortOrder
public int compare(Object arg
}
具體的排序規則對象
/** */ /**
*
*/
package com
/** */ /**
* @author Administrator
*
*/
public class EmployeeOrder
public final static int _LONG =
public final static int _STRING =
public final static int _DATE =
public final static int _BIGDECIMAL =
private String propertyName;
private boolean isAsc;
private int dataType;
public EmployeeOrder(String propertyName
}
public boolean isAsc()
return isAsc;
}
public void setAsc( boolean isAsc)
}
public String getPropertyName()
return propertyName;
}
public void setPropertyName(String propertyName)
}
public int getDataType()
return dataType;
}
public void setDataType( int dataType)
}
}
這裡是重點
/** */ /**
*
*/
package com
import java
/** */ /**
* @author Administrator
*
*/
public class EmployeeSortOrder implements ISortOrder
private List < EmployeeOrder > orders;
public EmployeeSortOrder(List < EmployeeOrder > orders)
}
public int compare(Object arg
int result =
try
Employee e
Employee e
for (EmployeeOrder order : orders)
result *=
}
if (result !=
break ;
}
}
} catch (Exception e)
// TODO: handle exception
}
return result;
}
private int sort(Object v
int result =
switch (dataType)
String s
String s
result = pareTo(s
break ;
case EmployeeOrder
BigDecimal d
BigDecimal d
result = pareTo(d
break ;
case EmployeeOrder
Long l
Long l
result = pareTo(l
break ;
default :
result =
break ;
}
return result;
}
private Object getVaule(Object obj
Object result = null ;
try
}
return result;
}
}
沒多說的
package com
import java
import junit
public class EmployeeSortTest extends TestCase
private List < Employee > employeeList;
@Override
protected void setUp() throws Exception
Employee e;
Date date;
Calendar cal = Calendar
employeeList = new ArrayList < Employee > ();
for ( int i =
e = new Employee();
if (
cal
}
}
@Override
protected void tearDown() throws Exception
}
public void testSort()
List < EmployeeOrder > orders = new ArrayList < EmployeeOrder > ();
EmployeeOrder order = new EmployeeOrder(
ISortOrder sortOrder = new EmployeeSortOrder(orders);
EmployeeComparator comparator = new EmployeeComparator(sortOrder);
Collections
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25943.html